有这样的方法吗
int a = (b == 5) ? c : d;
使用Bash ?
有这样的方法吗
int a = (b == 5) ? c : d;
使用Bash ?
当前回答
还有两个答案
这里有一些思考的方法
Bash整数变量
此外,dutCh, Vladimir和ghostdog74的纠正答案,因为这个问题是关于整数和带标签的bash:
有这样的方法吗 Int a = (b == 5) ?C: d;
在bash下使用整数有一个很好的方法:
declare -i b=' RANDOM % 3 + 4 ' c=100 d=50 a=' b == 5 ? c : d '; echo $b '-->' $a
这个命令的输出行应该是:
4 --> 50
5 --> 100
6 --> 50
当然,声明整型变量只需要执行一次:
declare -i a b c d
c=100 d=50 b=RANDOM%3+4
a=' b == 5 ? c : d '
echo $a $b
100 5
b=12 a=b==5?c:d
echo $a $b
50 12
题外话:使用字符串作为数学函数:
mathString=' b == 5 ? c : d '
b=5 a=$mathString
echo $a $b
100 5
b=1 a=$mathString
echo $a $b
50 1
基于参数展开和间接
以下是Brad Parks和druid62的回答,这里有一种不局限于整数的方法:
c=50 d=100 ar=([5]=c)
read -p 'Enter B: ' b
e=${ar[b]:-d};echo ${!e}
如果b==5,那么ar[b]是c,间接做c是50。 否则ar[除5之外的任何值]为空,因此参数展开将默认为d,其中间接给出100。
同样的演示使用数组代替整数
ternArrayDemo(){
local -a c=(foo bar) d=(foo bar baz) e=(empty) ar=([5]=c [2]=d)
local b=${ar[$1]:-e}
b+=[@] # For array indirection
printf ' - %s\n' "${!b}"
}
Then
ternArrayDemo 0
- empty
ternArrayDemo 2
- foo
- bar
- baz
ternArrayDemo 4
- empty
ternArrayDemo 5
- foo
- bar
ternArrayDemo 6
- empty
或者使用关联数组
ternAssocArrayDemo(){
local -a c=(foo bar) d=(foo bar baz) e=(empty)
local -A ar=([foo]=c[@] [bar]=d[@] [baz]=d[-1])
local b=${ar[$1]:-e[@]}
printf ' - %s\n' "${!b}"
}
Then
ternAssocArrayDemo hello
- empty
ternAssocArrayDemo foo
- foo
- bar
ternAssocArrayDemo bar
- foo
- bar
- baz
ternAssocArrayDemo baz
- baz
其他回答
这里有另一个选项,你只需要指定你分配一次的变量,不管你分配的是字符串还是数字:
VARIABLE=`[ test ] && echo VALUE_A || echo VALUE_B`
只是一个想法。:)
在bash中还有一个非常相似但更简单的三元条件语句语法:
a=$(( b == 5 ? 123 : 321 ))
简单的三元
brew list | grep -q bat && echo 'yes' || echo 'no'
此示例将确定您是否使用自制程序安装bat
如果是,你会看到"是"
如果是false,你会看到"no"
我在这里添加了-q来抑制grepped字符串输出,所以你只能看到“yes”或“no”
真正的模式是这样的
doSomethingAndCheckTruth && echo 'yes' || echo 'no'
用bash和zsh测试
if [[ $b -eq 5 ]]; then a="$c"; else a="$d"; fi
其他答案中建议的cond && op1 || op2表达式有一个固有的错误:如果op1有一个非零的退出状态,op2将静默地成为结果;在-e模式下也不会捕获错误。因此,该表达式只有在op1永远不会失败的情况下才安全使用(例如,:,如果是内置的,true,或者没有任何可能失败的操作(如除法和OS调用)的变量赋值)。
注意“”引号。它们将防止将所有空白转换为单个空格。
如果$b等于一个测试操作符(例如,如果$b等于一个测试操作符,则双方括号与单方括号相反,可以防止错误的操作。“- z”;一个变通方法是[是["x$b" == "xyes"],它只适用于字符串比较);他们还取消了报价的要求。
有些人已经提出了一些不错的替代方案。我想让语法尽可能接近,所以我写了一个名为?的函数。
这允许使用以下语法:
[[ $x -eq 1 ]]; ? ./script1 : ./script2
# or
? '[[ $x -eq 1 ]]' ./script1 : ./script2
在这两种情况下,:都是可选的。所有有空格的参数都必须加引号,因为它使用eval运行它们。
如果<then>或<else>子句不是命令,则函数返回正确的值。
./script; ? Success! : "Failure :("
这个函数
?() {
local lastRet=$?
if [[ $1 == --help || $1 == -? ]]; then
echo $'\e[37;1mUsage:\e[0m
? [<condition>] <then> [:] <else>
If \e[37;1m<then>\e[0m and/or \e[37;1m<else>\e[0m are not valid commands, then their values are
printed to stdOut, otherwise they are executed. If \e[37;1m<condition>\e[0m is not
specified, evaluates the return code ($?) of the previous statement.
\e[37;1mExamples:\e[0m
myVar=$(? "[[ $x -eq 1 ]] foo bar)
\e[32;2m# myVar is set to "foo" if x is 1, else it is set to "bar"\e[0m
? "[[ $x = *foo* ]] "cat hello.txt" : "cat goodbye.txt"
\e[32;2m# runs cat on "hello.txt" if x contains the word "foo", else runs cat on
# "goodbye.txt"\e[0m
? "[[ $x -eq 1 ]] "./script1" "./script2"; ? "Succeeded!" "Failed :("
\e[32;2m# If x = 1, runs script1, else script2. If the run script succeeds, prints
# "Succeeded!", else prints "failed".\e[0m'
return
elif ! [[ $# -eq 2 || $# -eq 3 || $# -eq 4 && $3 == ':' ]]; then
1>&2 echo $'\e[37;1m?\e[0m requires 2 to 4 arguments
\e[37;1mUsage\e[0m: ? [<condition>] <then> [:] <else>
Run \e[37;1m? --help\e[0m for more details'
return 1
fi
local cmd
if [[ $# -eq 2 || $# -eq 3 && $2 == ':' ]]; then
cmd="[[ $lastRet -eq 0 ]]"
else
cmd="$1"
shift
fi
if [[ $2 == ':' ]]; then
eval "set -- '$1' '$3'"
fi
local result=$(eval "$cmd" && echo "$1" || echo "$2")
if command -v ${result[0]} &> /dev/null; then
eval "${result[@]}"
else
echo "${result[@]}"
fi
}
显然,如果您希望脚本更短,您可以删除帮助文本。
编辑:我不知道?在文件名中充当占位符。它不像*那样匹配任意数量的字符,而是只匹配一个字符。因此,如果您的工作目录中有一个只有一个字符的文件,bash将尝试将文件名作为命令运行。我不知道怎么解决这个问题。我认为使用命令?...Args可能会工作,但是,没有骰子。