在$ {var?}中,bash变量参数扩展中的问号是什么意思? [英] What is the meaning of a question mark in bash variable parameter expansion as in ${var?}?

查看:112
本文介绍了在$ {var?}中,bash变量参数扩展中的问号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样使用bash变量的含义是什么

What is the meaning of a bash variable used like this:

 ${Server?}

推荐答案

它的工作原理几乎与(来自bash联机帮助页):

It works almost the same as (from the bash manpage):

${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).如果是这样,它将使用它.如果不是,它将输出word指定的错误消息(如果没有word,则输出适当的错误消息)并终止脚本.

That particular variant checks to ensure the variable exists (is both defined and not null). If so, it uses it. If not, it outputs the error message specified by word (or a suitable one if there is no word) and terminates the script.

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

The actual difference between that and the non-colon version can be found in the bash manpage above the section quoted:

当不执行子字符串扩展时,使用下面记录的形式,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.

换句话说,可以修改上面的部分以读取(基本上去掉"null"位):

In other words, the section above can be modified to read (basically taking out the "null" bits):

${parameter?word}
Display Error if Unset. If parameter is 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.

因此说明了区别:

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> _

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

There, you can see that while both unset and null variable result in an error with :?, only the unset one errors with ?.

这篇关于在$ {var?}中,bash变量参数扩展中的问号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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