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

发布于 2024-12-27 14:52:50 字数 66 浏览 1 评论 0原文

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

 ${Server?}

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

 ${Server?}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩人痒 2025-01-03 14:52:50

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

${parameter:?word}
如果为 Null 或未设置,则显示错误。如果参数为 null 或未设置,则 word 的扩展(或者如果 word 不存在则显示一条消息)将写入标准错误,并且 shell(如果不是交互式的)将退出。否则,将替换参数的值。

该特定变体检查以确保变量存在(已定义且不为空)。如果是这样,它就会使用它。如果没有,它会输出由 word 指定的错误消息(如果没有 word,则输出合适的错误消息)并终止脚本。

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

当不执行子字符串扩展时,使用下面记录的形式,bash 测试未设置或为 null 的参数。 省略冒号会导致仅测试未设置的参数。

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

${parameter?word}
如果未设置,则显示错误。如果未设置参数,则 word 的扩展(或者如果 word 不存在则显示一条消息)将写入标准错误,并且 shell(如果不是交互式的)将退出。否则,将替换参数的值。

差异如下所示:

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 变量都会导致 :?,只有未设置的一个错误为 ?

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.

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.

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

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.

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.

The difference is illustrated thus:

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

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

来世叙缘 2025-01-03 14:52:50

这意味着如果未定义变量,脚本应中止

示例:

#!/bin/bash
echo We will see this
${Server?Oh no! server is undefined!}
echo Should not get here

此脚本将打印出第一个回显,以及“哦不!...”错误消息。

在这里查看 bash 的所有变量替换:
https://www.gnu.org/software/ bash/manual/html_node/Shell-Parameter-Expansion.html

It means that the script should abort if the variable isn't defined

Example:

#!/bin/bash
echo We will see this
${Server?Oh no! server is undefined!}
echo Should not get here

This script will print out the first echo, and the "Oh no! ..." error message.

See all the variable substitutions for bash here:
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

つ低調成傷 2025-01-03 14:52:50

根据 man bash

  • ${parameter:?word}

如果参数为 null 或未设置,则 word 的扩展(或者如果 word 不存在则显示一条消息)将写入标准错误,并且 shell(如果不是交互式的)将退出。否则,将替换参数的值。

因此,如果您省略 word 并且不将 $1 传递给您的函数或 shell 脚本,那么它将退出并显示此错误:

-bash: 1: parameter null or not set

但是您可以放置​​一个用户定义的错误来让您的调用者知道尚未设置必需的参数,例如:

local arg1="${1:?needs an argument}"

仅当设置了 $1 时,它才会继续运行脚本/函数。

As per man bash:

  • ${parameter:?word}

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.

So if you omit word and don't pass $1 to your function or shell script then it will exit with this error:

-bash: 1: parameter null or not set

However you can place a user-defined error to let your caller know that a required argument has not been set e.g.:

local arg1="${1:?needs an argument}"

It will continue running script/function only if $1 is set.

等数载,海棠开 2025-01-03 14:52:50

如果给定参数为 null 或未设置,:? 会导致(非交互式)shell 退出并显示错误消息。您正在查看的脚本省略了错误消息;通常,您会看到类似的内容

foo=${1:?Missing first argument}

例如,

$ foo=${1:?Missing first argument}
bash: 1: Missing first argument
$ set firstarg
$ foo=${1:?Missing first argument}
$ echo $foo
firstarg

:? causes the (non-interactive) shell to exit with an error message if given parameter is null or unset. The script you are looking at is omitting the error message; usually, you'd see something like

foo=${1:?Missing first argument}

For example,

$ foo=${1:?Missing first argument}
bash: 1: Missing first argument
$ set firstarg
$ foo=${1:?Missing first argument}
$ echo $foo
firstarg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文