简体   繁体   English

如何在shell脚本中显示参数

[英]How to display argument in shell script

I have a shell script called displayArg.sh This is how I intend to run it- 我有一个名为displayArg.sh的shell脚本这就是我打算运行它的方式 -

./displayArg hello

and the output is entered arg is hello 并输入输出arg是你好

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 我是Shell脚本的新手

You want: 你要:

value="$1"

( $? is the status of the last command, which is 1 because the test command is what was executed last.) $?是最后一个命令的状态,为1,因为测试命令是最后执行的命令。)

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. 请注意测试中"$1"左右的引号。 If the string is empty, you get a syntax error. 如果字符串为空,则会出现语法错误。 Your alternative with bash is to use a [[ $1 == "" ]] test. 使用bash替代方法是使用[[ $1 == "" ]]测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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