Useful tips for bash scripting

From wiki.techunit.org
(Redirected from Bash)
Jump to: navigation, search

Write to multiple files

tee $FILE1 $FILE2 << EOF &> /dev/null
client
dev tun
proto $PROTO
remote $SRV $PORT
log-append /var/log/openvpn.log
resolv-retry infinite
cipher AES-256-CBC
key-direction 1
nobind
persist-key
persist-tun
comp-lzo
verb 3
EOF

Get options

  • example
while getopts "vn:h" OPTION
do
        case $OPTION in
        v)
                #Verbose mode
                #...
                ;;
        n)
                #Get param
                NAME=$OPTARG
                ;;
        h)
                #display help
                echo -e $ERRORMESSAGE
                exit 0
                ;;
        esac
done

Format date

DATE=$(date +%Y-%m-%d)

Function

function hello_world
{
   echo "hello world"
}

hello_world

Increment variable

COUNTER=0
((COUNTER++))

Elliot
techUnit's cofounder