Bash 语法错误:应为操作数(错误标记为“x2B”)
我正在用bash编写脚本,出现以下错误:Bash 语法错误:应为操作数(错误标记为“x2B”),bash,unix,Bash,Unix,我正在用bash编写脚本,出现以下错误: ./P4.1: line 10: +: syntax error: operand expected (error token is "+") 这是我的代码: #!/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 ]
./P4.1: line 10: +: syntax error: operand expected (error token is "+")
这是我的代码:
#!/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
出了什么问题,我该如何解决?
谢谢
此外,请引用并用括号括起变量:D使用圆括号进行数值计算:
num1=$((num1 + num2))
ceving和Tomek的组合:
#!/bin/bash
read num1 num2 num3
while [ $num1 -lt $num3 ]
do
echo $num1
num1=$((num1+num2))
done
同时检查
-gt
部分,是否应该是-lt
?是的,字符串似乎是问题所在。我需要将输入作为一个完整的字符串,然后将其拆分。为什么像我所说的那样使用$(())而不是$[]呢?好吧,两种方法都是正确的。你可以做你选择的任何一个。这应该读取数字,然后把它们分开。它一次读取所有数字,以空格分隔,并且在读取时已将它们分隔开。或者:for((;num1
#!/bin/bash
read num1 num2 num3
while [ $num1 -lt $num3 ]
do
echo $num1
num1=$((num1+num2))
done