简体   繁体   中英

Usage function not working as expected

I am writing a quick function to display some help type information however it always runs regardless of whether an argument is given or not:

help(){
     if [ $# -eq 0 ] ; then
     echo ''
     echo '########################################################'
     echo ''
     echo 'Argument to run run name must be given: ./report.sh Name'
     echo ''
     echo 'Name can be:'
     echo ''
     ALLNAMES=$(awk -F'|' '{print $1}' $CONFIGFILE)
     echo "$ALLNAMES"
     echo ''
     echo '########################################################'
     echo ''
     exit 1
     fi

}

From what I have research so far the conditional statement is correct and I am simply passing the argument like:

scriptname.sh argument1

The same result is given whether an argument is given or not.

You forgot to pass the arguments to the function as well, or perhaps call the function:

help "$@"

As another note it's better to use a different name for your function since help is already used for a builtin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM