是否可以将条件语句插入到诸如bash echo之类的打印命令中?
例如(不起作用)
$ cat t.sh
#!/bin/bash
string1="It's a "
string2opt1="beautiful"
string2opt2="gross"
string3=" day."
read c
echo -n "$string1 $( [[ $c -eq 0 ]] && { echo -n "$string2opt1" } || { echo "$string2opt2" } ) $string3"
我知道分号/非单行代码可以实现;我只是想知道是否有更优雅或更被接受的方式。
为了澄清,您想实现这一点:
#!/bin/bash
string1="It's a"
string2opt1="beautiful"
string2opt2="gross"
string3="day."
read -r c
if [[ $c -eq 0 ]]; then
echo "$string1" "$string2opt1" "$string3"
else
echo "$string1" "$string2opt2" "$string3"
fi
但只有一线。这对您有用吗?
#!/bin/bash
string1="It's a"
string2opt1="beautiful"
string2opt2="gross"
string3="day."
read -r c
echo "$string1" "$([[ $c -eq 0 ]] && echo "$string2opt1" || echo "$string2opt2")" "$string3"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句