相关疑难解决方法(0)

理解“IFS= read -r line”

我显然明白可以为内部字段分隔符变量添加值。例如:

$ IFS=blah
$ echo "$IFS"
blah
$ 
Run Code Online (Sandbox Code Playgroud)

我也明白这read -r line会将数据保存stdin到名为的变量line

$ read -r line <<< blah
$ echo "$line"
blah
$ 
Run Code Online (Sandbox Code Playgroud)

但是,命令如何分配变量值?它是否首先存储来自stdinto 变量的数据line,然后赋予lineto 的值IFS

bash shell-script

94
推荐指数
3
解决办法
10万
查看次数

`while read -r line || 是什么意思 [[ -n $line ]]` 是什么意思?

不久前我发现了一些从文件中读取输入的代码,我相信来自 Stack Exchange,我能够适应我的需求:

while read -r line || [[ -n "$line" ]]; do
    if [[ $line != "" ]]
    then
        ((x++));
        echo "$x:  $line"
    <then do something with $line>
    fi
done < "$1"
Run Code Online (Sandbox Code Playgroud)

我现在正在检查我的脚本并试图了解它在做什么......我不明白这个语句在做什么:

while read -r line || [[ -n "$line" ]];

我知道 -r 选项表示我们正在将原始文本读入行,但我|| [[ -n "$line" ]]对语句的部分感到困惑。有人可以解释一下这是在做什么吗?

read

8
推荐指数
1
解决办法
3087
查看次数

bash set -o errexit 问题还是变量递增的方式?

我有一个 shell 脚本,使用 set -e 递增变量,如下例所示:

$ var=0; echo $?
0
$ ((var++)); echo $?
1
$ ((var++)); echo $?
0
$ ((var++)); echo $?
0
$ echo $var; echo $?
3
0
$ bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later  <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.    
$ …
Run Code Online (Sandbox Code Playgroud)

shell bash shell-script

3
推荐指数
1
解决办法
1237
查看次数

标签 统计

bash ×2

shell-script ×2

read ×1

shell ×1