Bash 可以使用三元运算符为多个变量设置值吗?
Bash Can the ternary operator be used to set values to multiple variables?
我想做的(伪)代码:
n=100
desired=""
for i in {1..10} ; do
(( $(numeric_command_output) < n ?
set n to the output and desired to $i :
keep n and desired unchanged )) ; done
我知道如何使用三元运算符根据条件操作一个变量,但这可以在 bash 中完成吗?
Can the ternary operator be used to set values to multiple variables?
是的。
but can this be done in bash?
是的。
(( ( tmp=$(numeric_command_output) ) < n ? (n=tmp, desired=$i) : 0 ))
或
(( tmp=$(numeric_command_output, tmp < n ? (n=tmp, desired=$i) : 0 ))
我想做的(伪)代码:
n=100
desired=""
for i in {1..10} ; do
(( $(numeric_command_output) < n ?
set n to the output and desired to $i :
keep n and desired unchanged )) ; done
我知道如何使用三元运算符根据条件操作一个变量,但这可以在 bash 中完成吗?
Can the ternary operator be used to set values to multiple variables?
是的。
but can this be done in bash?
是的。
(( ( tmp=$(numeric_command_output) ) < n ? (n=tmp, desired=$i) : 0 ))
或
(( tmp=$(numeric_command_output, tmp < n ? (n=tmp, desired=$i) : 0 ))