如何在 bash 中构建条件赋值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2440947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to build a conditional assignment in bash?
提问by Neuquino
I'm looking a way to build conditional assignments in bash:
我正在寻找一种在 bash 中构建条件赋值的方法:
In Java it looks like this:
在 Java 中,它看起来像这样:
int variable= (condition) ? 1 : 0;
回答by Demosthenex
If you want a way to define defaults in a shell script, use code like this:
如果您想要一种在 shell 脚本中定义默认值的方法,请使用如下代码:
: ${VAR:="default"}
Yes, the line begins with ':'. I use this in shell scripts so I can override variables in ENV, or use the default.
是的,该行以“:”开头。我在 shell 脚本中使用它,所以我可以覆盖 ENV 中的变量,或者使用默认值。
This is related because this is my most common use case for that kind of logic. ;]
这是相关的,因为这是我对这种逻辑最常见的用例。;]
回答by Anthony Forloney
As per Jonathan's comment:
根据乔纳森的评论:
variable=$(( 1 == 1 ? 1 : 0 ))
EDIT:
编辑:
I revised the original answer which just echo
'd the value of the condition operator, it didn't actually show any assignment.
我修改了原始答案,它只是echo
'd'条件运算符的值,它实际上没有显示任何赋值。
回答by Kevin Little
myvar="default" && [[ <some_condition_is_true> ]] && myvar="non-default"
real examples:
真实例子:
DELIM="" && [[ "$APP_ENV_RESOLVED" != "" ]] && DELIM=$INNER_DELIM
The condition can be "(( ... ))" as well:
条件也可以是“(( ... ))”:
filepath=/proc/drbd && (( $# > 0 )) && filepath=
回答by Martin Rüegg
In addition to the other more general answers (particularly as per Jonathan's commentand Kevin's more general answer[which also supports strings]) I'd like to add the following two solutions:
除了其他更一般的答案(特别是根据乔纳森的评论和凯文更一般的答案[也支持字符串]),我想添加以下两个解决方案:
setting the variable to either 0
or 1
based on the condition:
将变量设置为0
或1
基于条件:
(as the question's example suggests.)
(正如问题的例子所暗示的那样。)
The general form would read
一般形式将阅读
(condition); variable=$?;
where $variable
results in being either 0
or 1
and condition
can be any valid conditional expression.
where$variable
结果是0
or1
和condition
可以是任何有效的条件表达式。
E.g. checking a variable ...
例如检查一个变量...
[[ $variableToCheck == "$othervariable, string or number to match" ]]
variable=$?
... or checking a file's existence ...
... 或检查文件是否存在 ...
[ -f "$filepath" ]
fileExists=$?
... or checking the nummerical value of $myNumber
:
...或检查 的数值$myNumber
:
(( myNumber >= 1000000000 ))
is_huge_number=$?
The advantages of this solution is that
这种解决方案的优点是
- it supports arbitrary conditional expressions, including strings
(which are not supported in arithmetic expressionsof Jonathan's solution) - that
variable
gets declaredin any case, unlike in griffon's answer:[ -z "$variable" ] && variable="defaultValue"
Which would matter in case you want to namerefit later on (e.g. from within a function).
- 它支持任意条件表达式,包括字符串
(乔纳森解决方案的算术表达式不支持) - 这在任何情况下
variable
都会被声明,与 griffon 的回答不同:[ -z "$variable" ] && variable="defaultValue"
如果您稍后想对其进行命名(例如从函数内),这将很重要。
Please note:In Bash, the special variable $?
always contains the exit code of the previously executed statement (or statement block; see the man bashfor more details). As such, a positive result is generally represented by the value 0
, not 1
(See my comment below, thanks Assimilater for pointing it out). Thus, if the condition is true (e.g [[2 eq 2]]
) then $?=0
.
请注意:在 Bash 中,特殊变量$?
始终包含先前执行的语句(或语句块;有关详细信息,请参阅man bash)的退出代码。因此,积极的结果通常由 value 表示0
,而不是1
(请参阅下面我的评论,感谢 Assimilater指出)。因此,如果条件为真(例如[[2 eq 2]]
),则$?=0
。
If instead you need 0 or 1 in your variable (e.g. to print or do mathematical calculations) then you need to employ boolean negation using a leading exclamation mark (as pointed out by GypsySpellweaver in the commentsbelow): ( ! condition ); variable=$?
or ! ( condition ); variable=$?
. (However, readability in terms of what happens might be a bit less obvious.)
相反,如果您需要在变量中使用 0 或 1(例如打印或进行数学计算),那么您需要使用带有前导感叹号的布尔否定(正如 GypsySpellweaver 在下面的评论中指出的那样):( ! condition ); variable=$?
或! ( condition ); variable=$?
. (但是,就发生的事情而言,可读性可能不太明显。)
Another possible solutionby Jonathan would be variable=$(( 1 == 1 ? 1 : 0 ))
- which, however, is creating a subshell.
乔纳森的另一个可能的解决方案是variable=$(( 1 == 1 ? 1 : 0 ))
- 然而,它正在创建一个子外壳。
If you want to avoid the creation of a subshel, keep good readability or have arbitrary conditions, use one of the following solutions.
如果您想避免创建 subshel,保持良好的可读性或具有任意条件,请使用以下解决方案之一。
setting the variable to arbitrary values:
将变量设置为任意值:
as it is done in most other answers, it could adapted as follows:
正如在大多数其他答案中所做的那样,它可以调整如下:
(condition) \
&& variable=true \
|| variable=false
e.g as in
例如在
[[ $variableToCheck == "$othervariable, string or number to match" ]] \
&& variable="$valueIfTrue" \
|| variable="$valueIfFalse"
or to get 1 in a positive check, and 0 upon failure (like in the question's example):
或在正面检查中获得 1,在失败时获得 0(如问题示例中所示):
[[ $variableToCheck == "$othervariable, string or number to match" ]] \
&& variable=1 \
|| variable=0
(for the last example, - as already mentioned in the notes above - the same can be achieved with boolean negation using a leading exclamation mark:
(对于最后一个示例, - 正如上面的注释中已经提到的 - 可以通过使用前导感叹号的布尔否定来实现相同的效果:
[[ ! $variableToCheck == "$othervariable, string or number to match" ]]
variable=$?
The advantages of this solution is that
这种解决方案的优点是
- it might be considered a bit better readablethan Kevin's answer
myvar="default" && [[ <some_condition_is_true> ]] && myvar="non-default"
, and - the
$valueIfTrue
is conditionally evaluated onlyif needed,
which would matter in case you'd do something- with side-effect, like
variable=$((i++))
, or{ variable=$1; shift; }
- high computation, like
variable=$(find / -type f -name ListOfFilesWithThisNameOnMySystem)
- with side-effect, like
- is a bit shorterthan ghostdog74's answer
(which, however is great if you have multiple conditions!) - does not open a subshell as in Pierre's answer
- and as above:
- it supports arbitrary conditional expressions, including strings
(which are not supported in arithmetic expressionsof Jonathan's solution) - that
variable
gets declaredin any case, unlike in griffon's answer:[ -z "$variable" ] && variable="defaultValue"
Which would matter in case you want to namerefit later on (e.g. from within a function).
- it supports arbitrary conditional expressions, including strings
- 它可能被认为比凯文的答案更具可读性,并且
myvar="default" && [[ <some_condition_is_true> ]] && myvar="non-default"
- 在
$valueIfTrue
有条件评估仅在需要时,
它会在你做一些无关紧要的情况下- 有副作用,比如
variable=$((i++))
, 或者{ variable=$1; shift; }
- 高计算,如
variable=$(find / -type f -name ListOfFilesWithThisNameOnMySystem)
- 有副作用,比如
- 有点短比ghostdog74的答案
(这不过是伟大的,如果你有多个条件!) - 不像皮埃尔的回答那样打开子shell
- 和上面一样:
回答by Pierre Maoui
I wanted to do a conditional assignment with strings and I ended up with :
我想用字符串进行条件赋值,结果是:
SOWHAT=$([ "$MYVALUE" = "value" ] && echo "YES" || echo "NO")
回答by Alex Gray
Big upsto @Demosthenex and especially @Dennis Williamson for the shortest and easiestsolution I've seen. Leave it to bashto require a bunch of parenthesesfor a simple ternary assignment. Ahh, the 60s! And to put it all together in an example...
大起大落对@Demosthenex,尤其是@Dennis威廉姆森的最短和最简单的我见过的解决方案。将它留给 bash以需要一堆括号来进行简单的三元赋值。啊,60年代!并把它们放在一个例子中......
echo $BASHRULES; # not defined
# no output
: ${BASHRULES:="SCHOOL"} # assign the variable
echo $BASHRULES # check it
SCHOOL # correct answer
: ${BASHRULES="FOREVER?"} # notice the slightly different syntax for the conditional assignment
echo $BASHRULES # let's see what happened!
SCHOOL # unchanged! (it was already defined)
I wrote that a long time ago.. these days I'd probably get more excited over a solution like...
我很久以前写过……这些天我可能会对像……这样的解决方案感到更加兴奋。
PLATFORM=iphonesimulator
OTHERSDK=iphone && [[ $PLATFORM=~os ]] \
&& OTHERSDK+=simulator \
|| OTHERSDK+=os
$OTHERSDK
?iphoneos
$OTHERSDK
?iphoneos
回答by griffon vulture
If you want to assign a value unless variable is empty use this:
如果您想分配一个值,除非变量为空,请使用以下命令:
[ -z "$variable" ] && variable="defaultValue"
You can put as well, each other condition on the []
你也可以把对方的条件放在 []
回答by ghostdog74
another way
其它的办法
case "$variable" in
condition ) result=1;;
*) result=0;;
esac