0

displayArg.shというシェルスクリプトがあります。これを実行する方法です-

./displayArg hello

出力が入力されますargishello

以下はスクリプトです-

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

どうぞよろしくお願いいたします。シェルスクリプトは初めてです

4

1 に答える 1

2

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.

于 2012-12-06T04:17:08.703 に答える