“加冒号"是什么意思(“+:") 在 shell 脚本表达式中是什么意思? [英] What does "plus colon" ("+:") mean in shell script expressions?

查看:26
本文介绍了“加冒号"是什么意思(“+:") 在 shell 脚本表达式中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么意思?

if ${ac_cv_lib_lept_pixCreate+:} false; then :
  $as_echo_n "(cached) " >&6
else
  ac_check_lib_save_LIBS=$LIBS

看起来ac_cv_lib_lept_pixCreate是一些变量,那么+:是什么意思?

Looks like ac_cv_lib_lept_pixCreate is some variable, so what does +: mean?

在哪里可以学习大括号表达式的完整语法?这种语法的名称是什么?

Where to learn complete syntax of curly bracket expressions? What is the name of this syntax?

推荐答案

在加冒号"${...+:} 表达式中,只有 +在shell中有特殊的意义.在这种情况下,冒号只是一个字符串值,因此我们可以将该代码段编写为 ${...+":"}.

In the "plus colon" ${...+:} expression, only the + has special meaning in the shell. The colon is just a string value in this case, so we could write that snippet as ${...+":"}.

为方便起见,假设变量名为var,并考虑表达式:

For convenience, let's pretend the variable is called var, and consider the expression:

if ${var+:} false; then ...

如果shell变量$var存在,则将整个表达式替换为:,如果不存在,则返回一个空字符串.

If the shell variable $var exists, the entire expression is replaced with :, if not, it returns an empty string.

因此整个表达式 ${var+:} false 变为 : false(返回 true)或 false(返回 false).

Therefore the entire expression ${var+:} false becomes either : false (returning true) or false (returning false).

这归结为存在性测试,即使变量没有赋值也可能为真.

This comes down to a test for existence, which can be true even if the variable has no value assigned.

它非常神秘,但碰巧的是,它是为数不多的检验变量是否存在的测试之一,该变量在大多数(如果不是全部)Bourne 血统的 shell 中确实有效.

It is very cryptic, but as it happens, is one of the few tests for the existence of a variable that actually works in most, if not all, shells of Bourne descent.

可能的等价物:(用这里的任何变量名替换 var)

Possible equivalents: (substitute any variable name here for var)

if [[ ${var+"is_set"} == is_set ]]; then ...

或者,可能更便携:

case ${var+"IS_SET"} in IS_SET) ...;; esac

这篇关于“加冒号"是什么意思(“+:") 在 shell 脚本表达式中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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