A bit about

Hello, everyone! All you can see below is just my bank of information. Some material I've found in the fathomless net, some I've learned myself. Don't think all of the information here is right or actual, but may be it could be of use for you :) All feedback is welcome, especially constructive ones :)

Friday, September 30, 2011

BASH: check new line at end of file

Sometimes it's useful to check if a file had newline at the end. For example, if there're lots of C source code (compiler shows warning if no newline found) or file is going to be processed by "cat filename | while read;" construction. The last one will not read the bottom line of the file.

I found several answers in Google, they used "sed" or "tail -n 1", but didn't work me.

I used the following construction:
if [ -s filename -a "$(tail -c 1 filename)" = "" ]; then
echo "Has newline";
else
echo "No newline";
fi

No comments:

Post a Comment