简体   繁体   English

Bash Shell脚本变量

[英]Bash shell scripting variables

I have the following line in my shell script: 我的shell脚本中有以下行:

if [ -n "${USE_X:+1}" ]; 如果[-n“ $ {USE_X:+1}”]; then 然后

I cannot figure out what the ":+1" part means. 我无法弄清楚“:+1”部分的含义。 Any ideas? 有任何想法吗?

Have a look here . 在这里看看。 That url provides the following explanation: 该URL提供以下解释:

${parameter:+alt_value} $ {参数:+ alt_value}

If parameter set, use alt_value, else use null string. 如果设置了参数,则使用alt_value,否则使用空字符串。

and has the following example: 并具有以下示例:

echo
echo "###### \${parameter:+alt_value} ########"
echo

a=${param4:+xyz}
echo "a = $a"      # a =

param5=
a=${param5:+xyz}
echo "a = $a"      # a =
# Different result from   a=${param5+xyz}

param6=123
a=${param6:+xyz}
echo "a = $a"      # a = xyz

basically if $USE_X is set, the statement is evaluated to 1, otherwise null. 基本上,如果设置了$ USE_X,则该语句的计算结果为1,否则为null。 Probably similar to 大概类似于

if [ -z $USE_X ];
then
    echo 1
else
    echo ""
fi

from http://tldp.org/LDP/abs/html/parameter-substitution.html#PATTMATCHING : 来自http://tldp.org/LDP/abs/html/parameter-substitution.html#PATTMATCHING

${parameter+alt_value}, ${parameter:+alt_value}
If parameter set, use alt_value, else use null string.

Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null, see below.

since aioobe already answered the question itself, here's a way to search a long manpage like Bash's using a regex, using this question as an example: 由于aioobe已经回答了问题本身,因此这是一种使用正则表达式搜索像Bash一样长的联机帮助页的方法,以该问题为例:


/\{.*:\+

The first forward-slash puts less (the manpage viewer) into search mode; 第一个正斜杠将less (联机帮助页查看器)置于搜索模式; the regex says to search for a left bracket, followed by any amount of stuff, then a colon followed by a plus sign. 正则表达式表示先搜索左括号,然后搜索任意数量的内容,然后搜索冒号和加号。 The bracket and plus need to be escaped because they have special meaning to the regex parser. 方括号和加号需要转义,因为它们对正则表达式解析器具有特殊含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM