Determining whether the file end has a carriage shell

Outline

When reading the contents of the file, if the file format is not UNIX, and the last line of the file is not in a blank line in the script by while read cycle, then the time to read the last line is always unable to process the data correctly, the reason is because the carriage return line feed character issues, so in order to solve this problem, in simple pretreatment at the end of the file to determine whether there is a newline.

tail -n1 <your file> | wc -l

1 there is represented an output newline 0 output No Wrap

solve

eg:

if [ $(tail -n1 <your file> | wc -l) -eq 1 ];then
    echo "Newline at the end of the file"
fi
Published 87 original articles · won praise 28 · views 120 000 +

Guess you like

Origin blog.csdn.net/MakerCloud/article/details/103942258