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

查看:34
本文介绍了检查 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天全站免登陆