我正在练习使用命令read
:
$ ls * | while read line; do printf "test %sn" $line; done
test data1.txt
test data5.txt
test data6.txt
test data7.txt
它工作正常,但
$ ls * | while read line; do file $line; done
data1.txt: cannot open `data1.txt' (No such file or directory)
data5.txt: cannot open `data5.txt' (No such file or directory)
data6.txt: cannot open `data6.txt' (No such file or directory)
data7.txt: cannot open `data7.txt' (No such file or directory)
data8.txt: cannot open `data8.txt' (No such file or directory)
文件名是合法的
$ ls
data1.txt data5.txt data6.txt data7.txt data8.txt
我的代码有什么问题。
我怀疑您的文件不叫"data1.txt"
,实际上更像"data1.txt "
。请注意结尾处的细微空间。这很容易是由于脚本中的一个小错误造成的,也可能是使用了不可打印的字符。您可以使用ls --quoting-style=shell-escape
对此进行检查。
示例:
$ touch "data1.txt "
$ touch "data2.txt"
$ echo "a" > "data3.txt "
$ ls data* | while read line; do file $line; done
data1.txt: cannot open (No such file or directory)
data2.txt: empty
data3.txt: cannot open (No such file or directory)
^
Notice the missing spaces
这条单行有几个问题:
- 从不解析
ls
- 始终引用变量
read
删除所有前导和尾部空白字符<这实际上是这里的主要问题>这实际上是这里的主要问题>
所以,如果你想解决read
问题,你可以:
$ ls data* | while IFS= read -r line; do file "$line"; done
data1.txt : empty
data2.txt: empty
data3.txt : ASCII text
^
Notice the spaces, this is due to read
但正确的方法是:
$ for f in data*; do file "$f"; done
data1.txt : empty
data2.txt: empty
data3.txt : ASCII text