Shell Scripting Accepting Argv , getops , checking , echoing

Shell Scripting Accepting Argv , getops , checking , echoing

#!/bin/bash

#getopts checking for argv and define it into variables
#options that needs to receive parameter have to enter : after the option EG: f:u:d:
#In This Example h does not accept parameter. it execute the case only

while getopts u:d:p:f:h option

do
 case "${option}"
 in
 u) USER=${OPTARG};;
 d) DATE=${OPTARG};;
 p) PRODUCT=${OPTARG};;
 f) FORMAT=$OPTARG;;
 h) echo "FORMAT : ./bash_accept_argv.sh -u {username} -d {date} -p {product} -f {file_type} EG: ./bash_accept_argv.sh -u 'bot1 bot2' -d '2018-01-05' -p MOMOKO -f pdf "
    exit 1;; 
 ?) echo "FORMAT : ./bash_accept_argv.sh -u {username} -d {date} -p {product} -f {file_type} EG: ./bash_accept_argv.sh -u 'bot1 bot2' -d '2018-01-05' -p MOMOKO -f pdf "
    exit 1;; 
 esac
done

#Prevent Empty option passed into the script
#[[ -z ${USER} ]] - Check length (More than one word)
#[ -z ${FORMAT}]  - Check Length (one word)
if [[ -z ${USER} ]] || [[ -z ${DATE} ]] || [ -z ${PRODUCT} ] || [ -z ${FORMAT} ]
then
    echo "FORMAT : ./bash_accept_argv.sh -u {username} -d {date} -p {product} -f {file_type} EG: ./bash_accept_argv.sh -u 'bot1 bot2' -d '2018-01-05' -p MOMOKO -f pdf "
    exit 1
fi

    

#How to extract more than one data pass into the option
for subuser in $USER;do
    echo $subuser
done;
echo ${DATE}
echo ${PRODUCT}
echo ${FORMAT}

ref link : Click Here

Back To Top