Peer-reviewed code snippets that anyone can edit
A wiki for useful code snippets
add a new line at end of a text file in every C source
#!/bin/bash
 
 
if [ -z "$1" ]; then
    echo "$0"
    echo
    echo "is a command to add a new line at end of a text file"
    echo "if the last line of the file is not a new line already."
    echo
    echo "it is usefull to avoid some C compilers to give warning like"
    echo "'no newline at end of file'"
    echo
    echo sintax:    
    echo    $0 "[-v|-q] <file1> <file2> ..."
    echo   
    echo "   -v  : verbose mode. write info during processes"
    echo "   -q  : quiet mode; no output given (default)"
    echo
    exit 1
fi
 
case "$1" in
    -v )
    mode=v
    shift
    ;;
    -q )
    mode=q
    shift
    ;;
    *)
    mode=q
    ;;
 
esac
 
dirs="$*"
 
for F in $dirs; do
    LASTLINE=$(tail -n 1 "$F")
    if [ "$LASTLINE" != "" ]; then 
    echo >> "$F"
    if [ "$mode" = v ]; then
        echo "+ : $F" >&2
    fi
    else 
    if [ "$mode" = v ]; then
        echo "- : $F" >&2
    fi
    fi
done