Haui's bytes

news, diary, journal, whatever

Useful bash functions II

Most perl scripts I write start like this:

#!/usr/bin/perl
use strict;
use warnings;
#maybe the GPL or some other stuff...

Additionally it’s always required to set the “x flag” via chmod - all in all quite annoying. That’s why I added the function shown below to my ~/.bashrc

np() {
    gplfile=~/Dokumente/gpl.txt
    if [ $# -eq 0 ]
    then
        echo "filename required..."
        return
    fi
    if [ -e "$1" ]
    then
        echo "file already exists!"
        return
    fi
    touch "$1" || { echo "can't touch $1" ; return ; }
    echo "#!/usr/bin/perl" >> "$1"
    if [ "$2" = "gpl" ] && [ -e "$gplfile" ]
    then
        cat "$gplfile" >> "$1"
    fi
    echo "" >> "$1"
    echo "use strict;" >> "$1"
    echo "use warnings;" >> "$1"
    echo "use 5.10.0;" >> "$1"
    echo "use feature 'say';" >> "$1"
    echo "" >> "$1"
    chmod 700 "$1"
    [ $EDITOR ] || EDITOR=vim
    $EDITOR "$1"
}

Usage: np filename [gpl]