UsiScript
#!/bin/bash ## Original author = silfun1 ## description ## This template give an example of two arguments given to a script, with a getops loop that check and get the varibales ## 2 functions exists: one is the usage and the other named backup (create a backup copy of a file) ## A check if done to see if the arguments are empty or not. If the second argument (target file) is still empty, we create the target filename automatically ## # created by SH , and checked with http://www.shellcheck.net/ # modify on 01/01/2015 #-----VARIABLES----- PGR_NAME=$(basename $0) NOWDATE=$(date +"%Y%m%d") SRCFILE="" TRGFILE="" #-------FUNCTIONS---------- usage() { echo echo "$PGR_NAME -n
-p
" echo exit 1 } backup() { filesrc=$1 filetarget=$2 if [ "$filesrc" != "" ]; then if [ -f "$filesrc" ]; then cp $filesrc $filetarget if [ $? -eq 0 ] ; then echo "***** Backup done for the file $filesrc (copied to $filetarget)" fi else echo "The file $filesrc not found" fi else echo "The source filename is empty" fi } #-------END - FUNCTIONS---------- #-----MAIN----- if [ $# -eq 0 ]; then usage fi while getopts "h:n:p:" opt do case $opt in h) usage ;; n) SRCFILE=${OPTARG};; p) TRGFILE=${OPTARG};; *) usage ;; esac done # CHECK VARIABLES CONTENT if [ "$SRCFILE" = "" ] then echo "Enter the source file :" read SRCFILE fi if [ "$TRGFILE" = "" ] then echo "Enter the target file :" read TRGFILE fi if [ "$TRGFILE" = "" ] then TRGFILE=${SRCFILE}_$NOWDATE.bck fi backup $SRCFILE $TRGFILE