检查输入参数的存在在Bash shell脚本 [英] Check existence of input argument in a Bash shell script

查看:150
本文介绍了检查输入参数的存在在Bash shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查输入参数的存在。我有以下脚本:

I need to check the existence of an input argument. I have the following script:

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

我得到

[: : integer expression expected

我如何检查输入参数1先查看它是否存在?

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

推荐答案

有:

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

-z 开关是检验的$ 1的扩张是一个空字符串或没有。如果它是一个空字符串然后执行体

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

这篇关于检查输入参数的存在在Bash shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆