语法 - Bash中的三元运算符(?:)
有没有办法做这样的事情
int a = (b == 5) ? c : d;
用Bash?
14个解决方案
346 votes
ternary operator ? :是if/else的缩写
case "$b" in
5) a=$c ;;
*) a=$d ;;
esac
要么
[[ $b = 5 ]] && a="$c" || a="$d"
ghostdog74 answered 2019-01-30T08:38:03Z
265 votes
码:
a=$([ "$b" == 5 ] && echo "$c" || echo "$d")
Vladimir answered 2019-01-30T08:38:20Z
107 votes
如果条件仅仅是检查是否设置了变量,那么甚至还有一个更短的形式:
a=${VAR:-20}
如果设置为VAR,则将分配给a的值为VAR,否则将为其分配默认值20 - 这也可能是表达式的结果。
正如Alex在评论中指出的那样,这种方法在技术上被称为“参数扩展”。
nemesisfixx answered 2019-01-30T08:38:55Z
42 votes
if [ "$b" -eq 5 ]; then a="$c"; else a="$d"; fi
其他答案中建议的""表达式有一个固有的错误:如果$b具有非零退出状态,则op2将自动成为结果; 错误也不会在-e模式中捕获。 因此,如果op1永远不会失败(例如,:,true,如果内置或变量赋值没有任何可能失败的操作(如除法和OS调用)),那么该表达式只能安全使用。
请注意""报价。 如果$b为空或具有空格,则第一对将阻止语法错误。 其他人会阻止将所有空格转换为单个空格。
ivan_pozdeev answered 2019-01-30T08:39:25Z
39 votes
(( a = b==5 ? c : d )) # string + numeric
dutCh answered 2019-01-30T08:39:41Z
27 votes
[ $b == 5 ] && { a=$c; true; } || a=$d
这将避免在||之后执行该部分 在&&和 和||失败。
Sir Athos answered 2019-01-30T08:40:03Z
10 votes
这是另一个选项,您只需要指定一次分配的变量,无论您的分配是字符串还是数字都无关紧要:
VARIABLE=`[ test ] && echo VALUE_A || echo VALUE_B`
只是一个想法。:)
Jasonovich answered 2019-01-30T08:40:32Z
9 votes
let命令支持大多数基本运算符:
let a=b==5?c:d;
当然,这仅适用于分配变量; 它无法执行其他命令。
emu answered 2019-01-30T08:41:01Z
5 votes
(ping -c1 localhost&>/dev/null) && { echo "true"; } || { echo "false"; }
wibble answered 2019-01-30T08:41:16Z
5 votes
以下似乎适用于我的用例:
例子
$ tern 1 YES NO
YES
$ tern 0 YES NO
NO
$ tern 52 YES NO
YES
$ tern 52 YES NO 52
NO
并可以在如下的脚本中使用:
RESULT=$(tern 1 YES NO)
echo "The result is $RESULT"
燕鸥
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 [ "$1" == "help" ]
then
show_help
fi
if [ -z "$3" ]
then
show_help
fi
# Set a default value for what is "false" -> 0
FALSE_VALUE=${4:-0}
function main
{
if [ "$1" == "$FALSE_VALUE" ]; then
echo $3
exit;
fi;
echo $2
}
main "$1" "$2" "$3"
Brad Parks answered 2019-01-30T08:41:53Z
3 votes
如果您想要类似的语法,可以使用它
a=$(( $((b==5)) ? c : d ))
PRIYESH PATEL answered 2019-01-30T08:42:15Z
2 votes
以下是一些选项:
1-如果在一行中使用if else,则可以。
if [[ "$2" == "raiz" ]] || [[ "$2" == '.' ]]; then pasta=''; else pasta="$2"; fi
2-写一个这样的函数:
# Once upon a time, there was an 'iif' function in MS VB ...
function iif(){
# Echoes $2 if 1,banana,true,etc and $3 if false,null,0,''
case $1 in ''|false|FALSE|null|NULL|0) echo $3;;*) echo $2;;esac
}
像这样使用内部脚本
result=`iif "$expr" 'yes' 'no'`
# or even interpolating:
result=`iif "$expr" "positive" "negative, because $1 is not true"`
3-灵感来自案例答案,更灵活,一线使用是:
case "$expr" in ''|false|FALSE|null|NULL|0) echo "no...$expr";;*) echo "yep $expr";;esac
# Expression can be something like:
expr=`expr "$var1" '>' "$var2"`
Sergio Abreu answered 2019-01-30T08:43:04Z
2 votes
我们可以在Shell Scripting中使用以下三种方式来实现三元运算符:
[ $numVar == numVal ] && resVar="Yop" || resVar="Nop"
Or
resVar=$([ $numVar == numVal ] && echo "Yop" || echo "Nop")
Or
(( numVar == numVal ? (resVar=1) : (resVar=0) ))
Sujay U N answered 2019-01-30T08:43:26Z
1 votes
在bash中还有一个非常类似于三元条件的语法:
a=$(( b == 5 ? 123 : 321 ))
不幸的是,它只适用于数字 - 据我所知。
Andre Dias answered 2019-01-30T08:43:55Z