bash while while循环删除文本文件的最后一行

cap*_*ser 2 bash loops while-loop

当我捕获这个文件时,我得到6行(这是一个差异文件)

bash-3.00$ cat /tmp/voo
18633a18634
> sashabSTP
18634a18636
> sashatSTP
21545a21548
> yheebash-3.00$
Run Code Online (Sandbox Code Playgroud)

然而,当我逐行阅读时,我只得到5行.

bash-3.00$ while read line ; do echo $line ; done < /tmp/voo
18633a18634
> sashaSTP
18634a18636  
> sashatSTP
21545a21548
Run Code Online (Sandbox Code Playgroud)

或这个

bash-3.00$ cat /tmp/voo | while read line ; do  echo $line ; done
18633a18634
> sashabSTP
18634a18636
> sashatSTP
21545a21548
bash-3.00$
Run Code Online (Sandbox Code Playgroud)

我错过了while循环中的最后一行'yhee'.

Mar*_*c B 5

注意:

21545a21548
> yheebash-3.00$
      ^---- no line break
Run Code Online (Sandbox Code Playgroud)

您的文件不会以换行符结束.

  • 您还可以使用 `while read line || 为未终止的最后一行运行 `while` 循环 [ -n "$line" ]; 做`(见[这个以前的答案](http://stackoverflow.com/questions/12916352/shell-script-read-missing-last-line/12919766#12919766))。 (2认同)