有这样的方法吗
int a = (b == 5) ? c : d;
使用Bash ?
有这样的方法吗
int a = (b == 5) ? c : d;
使用Bash ?
当前回答
这样的方法怎么样:
# any your function
function check () {
echo 'checking...';
# Change the following to 'true' to emulate a successful execution.
# Note: You can replace check function with any function you wish.
# Be aware in linux false and true are funcitons themselves. see 'help false' for instance.
false;
}
# double check pattern
check && echo 'update' \
|| check || echo 'create';
看看条件语句是如何在RxJs中工作的(即过滤器管道)。 是的,从我的角度来看,这是代码复制,但更实用的方法。
其他回答
如果您想要类似的语法,您可以使用它
a=$(( $((b==5)) ? c : d ))
[ $b == 5 ] && { a=$c; true; } || a=$d
这将避免在&&和||之间的代码失败时意外执行||之后的部分。
let命令支持大多数基本操作符:
let a=b==5?c:d;
当然,这只适用于分配变量;不能执行其他命令。
(ping -c1 localhost&>/dev/null) && { echo "true"; } || { echo "false"; }
(( a = b==5 ? c : d )) # string + numeric