Search code examples
bashshellgetopts

Adding flags to my shell script


Run with bash IProject.sh

Want to add bash -h IProject.sh

With -h (human) I want it to print out more information.

My code is...

while getopts h name
do 
   case $name in
      h) humanOpt=1;;
   esac 
done

#TESTING HERE
echo "$humanOpt"    

if [[ ! -z $humanOpt]]; then
   human version
else
   computer version 
fi

However, I am only printing the statements in the else.. Why is that?


Solution

  • I guess you could figure yourself with a bit trial and error

    bash IProject.sh -h
    

    does the job. But you need to fill in a bit more space for bash to breathe

    if [[ ! -z $humanOpt ]]; then # here
    

    . Your first guess bash -h IProject.sh adds -h as an option to the bash command itself, not to the IProject.sh call.