我是Unix和Linux的新手,并且未能在if语句中进行逻辑比较。
我确定这是一个非常基本的错误,但我无法找到错误。
if (7+3 == 10); then
echo "nice this works"
elif (7+3 == 73); then
echo "too bad string concatenation"
else
echo "I really don't understand shell"
fi
回声:我真的不懂贝壳。
答案 0 :(得分:4)
我希望您能两次看到此错误消息:7+3: command not found
- 是吗?
单组括号在子shell中运行附带的命令,因此您尝试使用两个参数7+3
和==
(或10
执行命令73
})
算术评估发生在 double 括号内
if ((7+3 == 10)); then
echo "nice this works"
elif ((7+3 == 73)); then
echo "to bad string concatenation"
else
echo "I really don't understand shell"
fi
nice this works
答案 1 :(得分:0)
您正在使用的运算符==用于字符串比较。正确的方法是使用-eq(相等)运算符:
if [ 10 -eq 10 ]; then
echo "10 = 10"
fi
并且您还需要使用右括号作为if [](您可以使用'man test'在bash中查找此内容)
答案 2 :(得分:0)
正确的语法是
if [ $((7+3)) -eq 10 ]; then
echo "nice this works"