Shell script read missing last line

前端 未结 7 1304
逝去的感伤
逝去的感伤 2020-11-22 12:57

I have an ... odd issue with a bash shell script that I was hoping to get some insight on.

My team is working on a script that iterates through lines in a file and

7条回答
  •  天命终不由人
    2020-11-22 13:40

    According to the POSIX spec for the read command, it should return a nonzero status if "End-of-file was detected or an error occurred." Since EOF is detected as it reads the last "line", it sets $line and then returns an error status, and the error status prevents the loop from executing on that last "line". The solution is easy: make the loop execute if the read command succeeds OR if anything was read into $line.

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

提交回复
热议问题