有这样的方法吗
int a = (b == 5) ? c : d;
使用Bash ?
有这样的方法吗
int a = (b == 5) ? c : d;
使用Bash ?
当前回答
三元运算符?:是if/else的缩写形式
case "$b" in
5) a=$c ;;
*) a=$d ;;
esac
Or
[[ $b = 5 ]] && a="$c" || a="$d"
其他回答
在bash中还有一个非常相似但更简单的三元条件语句语法:
a=$(( b == 5 ? 123 : 321 ))
这是通解
也适用于字符串测试 感觉更像是一种表达 避免在条件失效时产生任何微妙的副作用
数值比较试验
a=$(if [ "$b" -eq 5 ]; then echo "$c"; else echo "$d"; fi)
字符串比较测试
a=$(if [ "$b" = "5" ]; then echo "$c"; else echo "$d"; fi)
let命令支持大多数基本操作符:
let a=b==5?c:d;
当然,这只适用于分配变量;不能执行其他命令。
这和弗拉基米尔的回答很像。如果你的“三元”是一个“如果真,字符串,如果假,空”的情况下,那么你可以简单地做:
$ c="it was five"
$ b=3
$ a="$([[ $b -eq 5 ]] && echo "$c")"
$ echo $a
$ b=5
$ a="$([[ $b -eq 5 ]] && echo "$c")"
$ echo $a
it was five
上面的答案[[$b = 5]] && a="$c" || a="$d"应该只在你确定&&之后没有错误的情况下使用,否则它将错误地执行||之后的部分。
为了解决这个问题,我写了一个三元函数,它的行为应该,它甚至使用?和:操作符:
编辑-新的解决方案
这是我的新解决方案,不使用$IFS或ev(a/i)l。
function executeCmds()
{
declare s s1 s2 i j k
declare -A cmdParts
declare pIFS=$IFS
IFS=$'\n'
declare results=($(echo "$1" | grep -oP '{ .*? }'))
IFS=$pIFS
s="$1"
for ((i=0; i < ${#results[@]}; i++)); do
s="${s/${results[$i]}/'\0'}"
results[$i]="${results[$i]:2:${#results[$i]}-3}"
results[$i]=$(echo ${results[$i]%%";"*})
done
s="$s&&"
let cmdParts[t]=0
while :; do
i=${cmdParts[t]}
let cmdParts[$i,t]=0
s1="${s%%"&&"*}||"
while :; do
j=${cmdParts[$i,t]}
let cmdParts[$i,$j,t]=0
s2="${s1%%"||"*};"
while :; do
cmdParts[$i,$j,${cmdParts[$i,$j,t]}]=$(echo ${s2%%";"*})
s2=${s2#*";"}
let cmdParts[$i,$j,t]++
[[ $s2 ]] && continue
break
done
s1=${s1#*"||"}
let cmdParts[$i,t]++
[[ $s1 ]] && continue
break
done
let cmdParts[t]++
s=${s#*"&&"}
[[ $s ]] && continue
break
done
declare lastError=0
declare skipNext=false
for ((i=0; i < ${cmdParts[t]}; i++ )) ; do
let j=0
while :; do
let k=0
while :; do
if $skipNext; then
skipNext=false
else
if [[ "${cmdParts[$i,$j,$k]}" == "\0" ]]; then
executeCmds "${results[0]}" && lastError=0 || lastError=1
results=("${results[@]:1}")
elif [[ "${cmdParts[$i,$j,$k]:0:1}" == "!" || "${cmdParts[$i,$j,$k]:0:1}" == "-" ]]; then
[ ${cmdParts[$i,$j,$k]} ] && lastError=0 || lastError=1
else
${cmdParts[$i,$j,$k]}
lastError=$?
fi
if (( k+1 < cmdParts[$i,$j,t] )); then
skipNext=false
elif (( j+1 < cmdParts[$i,t] )); then
(( lastError==0 )) && skipNext=true || skipNext=false
elif (( i+1 < cmdParts[t] )); then
(( lastError==0 )) && skipNext=false || skipNext=true
fi
fi
let k++
[[ $k<${cmdParts[$i,$j,t]} ]] || break
done
let j++
[[ $j<${cmdParts[$i,t]} ]] || break
done
done
return $lastError
}
function t()
{
declare commands="$@"
find="$(echo ?)"
replace='?'
commands="${commands/$find/$replace}"
readarray -d '?' -t statement <<< "$commands"
condition=${statement[0]}
readarray -d ':' -t statement <<< "${statement[1]}"
success="${statement[0]}"
failure="${statement[1]}"
executeCmds "$condition" || { executeCmds "$failure"; return; }
executeCmds "$success"
}
executeCmds将每个命令单独分开,除了由于&&和||操作符而应该跳过的命令。当命令以!开头时,它使用[]。或者一面旗帜。
向它传递命令有两种方式:
传递单个命令时不加引号,但一定要引用;、&&和||操作符。
t ls / ? ls qqq '||' echo aaa : echo bbb '&&' ls qq
传递所有引用的命令:
t 'ls /a ? ls qqq || echo aaa : echo bbb && ls qq'
注意,我发现没有办法将&&和||操作符作为不带引号的参数传递,因为它们是函数名和别名的非法字符,我也没有办法覆盖bash操作符。
旧的解决方案-使用ev(a/i)l
function t()
{
pIFS=$IFS
IFS="?"
read condition success <<< "$@"
IFS=":"
read success failure <<< "$success"
IFS=$pIFS
eval "$condition" || { eval "$failure" ; return; }
eval "$success"
}
t ls / ? ls qqq '||' echo aaa : echo bbb '&&' ls qq
t 'ls /a ? ls qqq || echo aaa : echo bbb && ls qq'