Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

bash - 检查Bash Shell脚本中输入参数的存在(Check existence of input argument in a Bash shell script)

I need to check the existence of an input argument.

(我需要检查输入参数的存在。)

I have the following script

(我有以下脚本)

if [ "$1" -gt "-1" ]
  then echo hi
fi

I get

(我懂了)

[: : integer expression expected

How do I check the input argument1 first to see if it exists?

(如何首先检查输入参数1是否存在?)

  ask by user775187 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is:

(它是:)

if [ $# -eq 0 ]
  then
    echo "No arguments supplied"
fi

The $# variable will tell you the number of input arguments the script was passed.

($#变量将告诉您脚本传递的输入参数的数量。)

Or you can check if an argument is an empty string or not like:

(或者您可以检查参数是否为空字符串,例如:)

if [ -z "$1" ]
  then
    echo "No argument supplied"
fi

The -z switch will test if the expansion of "$1" is a null string or not.

(-z开关将测试“ $ 1”的扩展名是否为空字符串。)

If it is a null string then the body is executed.

(如果为空字符串,则执行主体。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...