【发布时间】:2018-12-05 02:18:28
【问题描述】:
我正在尝试理解一个测试脚本,其中包括以下部分:
OIFS=$IFS;
IFS="|";
【问题讨论】:
-
IFS是内部字段分隔符;参见例如this question 及其接受的答案。OIFS只是存储了Old 的值,所以很容易重置它。
我正在尝试理解一个测试脚本,其中包括以下部分:
OIFS=$IFS;
IFS="|";
【问题讨论】:
IFS 是内部字段分隔符;参见例如this question 及其接受的答案。 OIFS 只是存储了Old 的值,所以很容易重置它。
这里的 OIFS 是一个用户定义的变量,用于备份当前的 Bash internal field separator 值。
然后将内部字段分隔符变量设置为用户定义的值,可能会启用某种依赖于它的解析/文本处理算法,并在脚本稍后的某个位置恢复到其原始值。
【讨论】:
在 shell 中,每当我们需要访问变量的值时,我们使用$variableName,而每当我们需要为变量赋值时,我们使用variableName=xxx。
因此:-
# here we assigning the value of $IFS(Internal Field Separator) in OIFS.
OIFS=$IFS;
# and here we are re-assigning some different value to IFS.
# it's more like, first we store old value of IFS variable and then assign new value to it. So that later we can use the old value if needed.
IFS="|";
【讨论】:
IFS 是内部字段分隔符。 sn-p 将 IFS 更改为“|”保存旧值后,以后可以恢复。
例子:
->array=(one two three)
->echo "${array[*]}"
one two three
->IFS='|'
->echo "${array[*]}"
one|two|three
【讨论】: