加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Bash 使用函数测试变量unset

发布时间:2020-12-15 09:11:11 所属栏目:安全 来源:网络整理
导读:一个简单的Bash变量测试: ${varName:? "${varName} is not defined"} 我想重用这个,通过把它放在一个函数。 怎么样? 以下失败 ## Test a variable existstvar(){ val=${1:? "${1} must be defined,preferably in $basedir"} if [ -z ${val} ] then echo Z
一个简单的Bash变量测试:
${varName:?    "${varName} is not defined"}

我想重用这个,通过把它放在一个函数。
怎么样?

以下失败

#
# Test a variable exists
tvar(){
 val=${1:?    "${1}    must be defined,preferably in $basedir"}
 if [ -z ${val}  ]
     then 
     echo Zero length value 
 else
     echo ${1} exists,value ${1}
 fi
}

也就是说如果测试失败,我需要退出。

感谢lhunath的回答,我被引导到Bash人页面的一部分,我忽略了几百次:
    When  not performing substring  expansion,bash tests for a parameter that
    is unset  or null; omitting the colon results in a test only for a parame                        

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读