Shorter bash way :
To use the entire file without changing it:
cat file <(echo) | wc -l
or
wc -l < <(cat file <(echo) )
To verify the correct completion of the file and fix it:
[ $(tail -n +$(( $(wc -l <$file) +1)) <$file | wc -c) -gt 0 ] && echo >>$file
Explanation: Skip the number of lines from the $ file than leading characters should be considered: must be 0 if the last line is complete.
Another approach:
[ $(tail -c1 $file | wc -l) -eq 0 ] && echo >>$file
Eplained: find the last character in the file, if it is not a new line, linecount from this will be 0.
F. Hauri
source share