Bash 中的三元运算符 (?:)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3953645/
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
Ternary operator (?:) in Bash
提问by En_t8
Is there a way to do something like this
有没有办法做这样的事情
int a = (b == 5) ? c : d;
using Bash?
使用 Bash?
回答by ghostdog74
ternary operator ? :
is just short form of if/else
三元运算符? :
只是if/else
case "$b" in
5) a=$c ;;
*) a=$d ;;
esac
Or
或者
[[ $b = 5 ]] && a="$c" || a="$d"
回答by Vladimir
Code:
代码:
a=$([ "$b" == 5 ] && echo "$c" || echo "$d")
回答by nemesisfixx
If the condition is merely checking if a variable is set, there's even a shorter form:
如果条件只是检查是否设置了变量,则还有更短的形式:
a=${VAR:-20}
will assign to a
the value of VAR
if VAR
is set, otherwise it will assign it the default value 20
-- this can also be a result of an expression.
将要分配给a
的值VAR
如果VAR
被设置,否则将分配给它的默认值20
-这也可以是一个表达的结果。
As alex notes in the comment, this approach is technically called "Parameter Expansion".
正如亚历克斯在评论中指出的那样,这种方法在技术上称为“参数扩展”。
回答by ivan_pozdeev
if [ "$b" -eq 5 ]; then a="$c"; else a="$d"; fi
The cond && op1 || op2
expression suggested in other answers has an inherent bug: if op1
has a nonzero exit status, op2
silently becomes the result; the error will also not be caught in -e
mode. So, that expression is only safe to use if op1
can never fail (e.g., :
, true
if a builtin, or variable assignment without any operations that can fail (like division and OS calls)).
cond && op1 || op2
其他答案中建议的表达式有一个固有的错误:如果op1
退出状态非零,则op2
默默地成为结果;错误也不会在-e
模式中被捕获。所以,这表达仅仅是安全的使用,如果op1
不能失败(例如:
,true
如果一个内置的,或者不能够失败(如部门和操作系统调用)的任何操作变量赋值)。
Note the ""
quotes. The first pair will prevent a syntax error if $b
is blank or has whitespace. Others will prevent translation of all whitespace into single spaces.
注意""
引号。如果第一对$b
为空白或有空格,则将防止语法错误。其他人会阻止将所有空格转换为单个空格。
回答by dutCh
(( a = b==5 ? c : d )) # string + numeric
回答by Sir Athos
[ $b == 5 ] && { a=$c; true; } || a=$d
This will avoid executing the part after || by accident when the code between && and || fails.
这将避免在 || 之后执行部分 && 和 || 之间的代码偶然出现 失败。
回答by emu
The letcommand supports most of the basic operators one would need:
该让命令支持基本算一个需要的最:
let a=b==5?c:d;
Naturally, this works only for assigning variables; it cannot execute other commands.
当然,这仅适用于分配变量;它不能执行其他命令。
回答by Jasonovich
Here is another option where you only have to specify the variable you're assigning once, and it doesn't matter whether what your assigning is a string or a number:
这是另一种选择,您只需指定一次要分配的变量,并且分配的内容是字符串还是数字都没有关系:
VARIABLE=`[ test ] && echo VALUE_A || echo VALUE_B`
Just a thought. :)
只是一个想法。:)
回答by Brad Parks
The following seems to work for my use cases:
以下似乎适用于我的用例:
Examples
例子
$ tern 1 YES NO
YES
$ tern 0 YES NO
NO
$ tern 52 YES NO
YES
$ tern 52 YES NO 52
NO
and can be used in a script like so:
并且可以在这样的脚本中使用:
RESULT=$(tern 1 YES NO)
echo "The result is $RESULT"
tern
燕鸥
function show_help()
{
echo ""
echo "usage: BOOLEAN VALUE_IF_TRUE VALUE_IF_FALSE {FALSE_VALUE}"
echo ""
echo "e.g. "
echo ""
echo "tern 1 YES NO => YES"
echo "tern 0 YES NO => NO"
echo "tern "" YES NO => NO"
echo "tern "ANY STRING THAT ISNT 1" YES NO => NO"
echo "ME=$(tern 0 YES NO) => ME contains NO"
echo ""
exit
}
if [ "" == "help" ]
then
show_help
fi
if [ -z "" ]
then
show_help
fi
# Set a default value for what is "false" -> 0
FALSE_VALUE=${4:-0}
function main
{
if [ "" == "$FALSE_VALUE" ]; then
echo
exit;
fi;
echo
}
main "" "" ""
回答by Sujay U N
We can use following three ways in Shell Scripting for ternary operator :
我们可以在 Shell Scripting 中使用以下三种方式进行三元运算符:
[ $numVar == numVal ] && resVar="Yop" || resVar="Nop"
Or
resVar=$([ $numVar == numVal ] && echo "Yop" || echo "Nop")
Or
(( numVar == numVal ? (resVar=1) : (resVar=0) ))