bash 语法错误:预期操作数(错误标记为“+”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20176640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:27:16 来源:igfitidea点击:
Syntax error: operand expected (error token is "+")
提问by shoham
I'm writing a script in bash and I get this error:
我正在用 bash 编写脚本,但出现此错误:
./P4.1: line 10: +: syntax error: operand expected (error token is "+")
And this is my code:
这是我的代码:
#!/bin/bash
read string
echo $string >| temp
num1= cut -d" " -f1 temp
num2= cut -d" " -f2 temp
num3= cut -d" " -f3 temp
while [ $num1 -gt $num3 ]
do
echo $num1
num1=$[$num1+$num2]
done
What's wrong and how do I fix it? Thanks.
出了什么问题,我该如何解决?谢谢。
采纳答案by Blue Ice
Combination of ceving and Tomek's:
ceving 和 Tomek 的组合:
#!/bin/bash
read num1 num2 num3
while [ $num1 -lt $num3 ]
do
echo $num1
num1=$((num1+num2))
done
回答by ceving
Use round parenthesis for numeric computations:
使用圆括号进行数值计算:
num1=$((num1 + num2))
回答by petrus4
#!/bin/bash
read string
echo "${string}" >| temp
num1= cut -d" " -f1 temp
num2= cut -d" " -f2 temp
num3= cut -d" " -f3 temp
while [ "${num1}" -gt "${num3}" ]
do
echo "${num1}"
num1=$(expr "${num1}" + 1)
done
also, quote and brace your variables. :D
此外,引用和括号你的变量。:D