假设我有以下Bash脚本:
原文链接:https://www.f2er.com/bash/390003.htmlwhile read SCRIPT_SOURCE_LINE; do echo "$SCRIPT_SOURCE_LINE" done
我注意到,对于没有换行符的文件末尾,这将有效地跳过最后一行。
我搜索了一个解决方案and found this:
When read reaches end-of-file instead
of end-of-line,it does read in the
data and assign it to the variables,
but it exits with a non-zero status.
If your loop is constructed “while
read ;do stuff ;doneSo instead of testing the read exit
status directly,test a flag,and have
the read command set that flag from
within the loop body. That way
regardless of reads exit status,the
entire loop body runs,because read
was just one of the list of commands
in the loop like any other,not a
deciding factor of if the loop will
get run at all.06001