Checking for the correct number of arguments

前端 未结 3 511
一向
一向 2020-12-04 08:02

How do i check for the correct number of arguments (one argument). If somebody tries to invoke the script without passing in the correct number of arguments, and checking to

3条回答
  •  再見小時候
    2020-12-04 08:49

    You can check the total number of arguments which are passed in command line with "$#" Say for Example my shell script name is hello.sh

    sh hello.sh hello-world
    # I am passing hello-world as argument in command line which will b considered as 1 argument 
    if [ $# -eq 1 ] 
    then
        echo $1
    else
        echo "invalid argument please pass only one argument "
    fi
    

    Output will be hello-world

提交回复
热议问题