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

查看:225
本文介绍了什么是“加冒号"? ("+:")在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 ...

如果存在外壳变量$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.

这是一个非常神秘的事物,但碰巧它是对变量的存在进行的为数不多的测试之一,该变量实际上可在大多数(如果不是全部)伯恩氏后裔的壳中起作用.

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天全站免登陆