How to use `while read` (Bash) to read the last line in a file if there’s no newline at the end of the file?

后端 未结 7 890
臣服心动
臣服心动 2020-12-02 13:58

Let’s say I have the following Bash script:

while read SCRIPT_SOURCE_LINE; do
  echo \"$SCRIPT_SOURCE_LINE\"
done

I noticed that for files

7条回答
  •  余生分开走
    2020-12-02 14:43

    In your first example, I'm assuming you are reading from stdin. To do the same with the second code block, you just have to remove the redirection and echo $REPLY:

    DONE=false
    until $DONE ;do
    read || DONE=true
    echo $REPLY
    done
    

提交回复
热议问题