The basic issue here is that read
will return errorlevel 1 when it encounters EOF, even if it'll still correctly feed the variable.
So you can use errorlevel of read
right away in your loop, otherwize, the last data won't be parsed. But you could do this:
eof= while [ -z "$eof" ]; do read SCRIPT_SOURCE_LINE || eof=true ## detect eof, but have a last round echo "$SCRIPT_SOURCE_LINE" done
If you want a very solid way to parse your lines, you should use:
IFS='' read -r LINE
Remember that:
- NUL character will be ignored
- if you stick to using
echo
to mimick the behavior ofcat
you'll need to force anecho -n
upon EOF detected (you can use the condition[ "$eof" == true ]
)
Read full article from 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? - Stack Overflow
No comments:
Post a Comment