UsiScript
getopts
Code
#Check the arguments given. while getopts "hip:" opt do case $opt in h) usage ;; i) MODE="info";; p) PATH="$OPTARG" ;; #$OPTARG contains string given in argument (like filename for example) *);; esac done
if arguments are presents
Code
#Check if there is arguments if [ $# -gt 0 ] then #Check if the argument $1 exist if [ "$1" != "" ]; then ARG1=$1 else ARG1="" fi else #usage() echo "No Arguments" exit 1 fi
check if there is argument and get the first argument
Code
NumOfMinParams=1 if [ $# -eq 0 ]; then echo "No arguments supplied" exit 1 fi if [ $# -lt $NumOfMinParams ]; then echo "We are waiting, at least $NumOfMinParams arguments" exit 1 fi if [ -z "$1" ]; then echo "argument empty" else varMYARG="$1" fi
while case opts loop
Code
#Check the arguments given by the user # PRG -option1 argument1 --option2 argument2 while [[ $# > 0 ]] do opt="$1" case $opt in -c) THECONFIG_FILE=$2 shift;; -h) usage ;; -p) PHPPREFIXBASE=$2 shift;; --version) PHPVERSION=$2 shift;; *) usage ;; esac shift done