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

在${var?}中,bash变量参数扩展中的问号的含义是什么?

发布时间:2020-12-15 18:49:05 所属栏目:安全 来源:网络整理
导读:这样一个bash变量的含义是什么? ${Server?} 它的工作原理与(从bash手册页)几乎相同: ${parameter:?word} Display Error if Null or Unset. If parameter is null or unset,the expansion of word (or a message to that effect if word is not present) is
这样一个bash变量的含义是什么?
${Server?}
它的工作原理与(从bash手册页)几乎相同:

${parameter:?word}
Display Error if Null or Unset. If parameter is null or unset,the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell,if it is not interactive,exits. Otherwise,the value of parameter is substituted.

该特定变体检查以确保变量存在(既定义又不为null).如果是这样,它会使用它.如果没有,则输出由字指定的错误消息(如果没有字,则输出适当的错误消息),并终止脚本.

这个和非冒号版本之间的实际区别可以在引用的部分的bash联机帮助页中找到:

When not performing substring expansion,using the forms documented below,bash tests for a parameter that is unset or null. Omitting the colon results in a test only for a parameter that is unset.

换句话说,上面的部分可以修改为读取(基本上取出“空”位):

${parameter?word}
Display Error if Unset. If parameter is unset,the value of parameter is substituted.

差异如此说明:

pax> unset xyzzy ; export plugh=

pax> echo ${xyzzy:?no}
bash: xyzzy: no

pax> echo ${plugh:?no}
bash: plugh: no

pax> echo ${xyzzy?no}
bash: xyzzy: no

pax> echo ${plugh?no}

pax> _

在那里,您可以看到,虽然unset和null变量都会导致错误:?,只有未设置的一个错误?

(编辑:李大同)

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

    推荐文章
      热点阅读