简体   繁体   中英

How to display argument in shell script

I have a shell script called displayArg.sh This is how I intend to run it-

./displayArg hello

and the output is entered arg is hello

The following is the script-

if [ $1 == "" ]; then
 default="Default"
 echo "no value is given. Output is $default"
else
 value=$?
 echo "entered arg is $value" #I know I am wrong in these 2 lines, but not sure how to fix it
fi

Kindly bear with me. I'm new to Shell scripting

You want:

value="$1"

( $? is the status of the last command, which is 1 because the test command is what was executed last.)

Or you can simplify to:

if [ "$1" == "" ]
then
    echo "no value is given. Output is Default"
else
    echo "entered arg is $1"
fi

Note the quotes around "$1" in the test. If the string is empty, you get a syntax error. Your alternative with bash is to use a [[ $1 == "" ]] test.

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