我想增加一个变量的值设置为23
。每次增加后,它应该打印出新值,这是我的代码。
a=23
while [ $a -lt 45 ];
do
$a = `expr $a + 1` ;
echo " the new value is $a ";
done
但是我得到这样的结果。
基本上没有增加。
有人可以更正代码吗?
答案 0 :(得分:2)
您在左分配语句中使用了$ sign,这不是您期望的。 请更改您的行
$a = `expr $a + 1` ;
到
a=`expr $a + 1`;
还请注意,bash脚本中不得使用=
符号前后的空格
更新: 此代码可以正常工作,不会出现 bash 或 sh 的语法错误:
a=23
while [ $a -lt 45 ]; do
a=`expr $a + 1`
echo "the new value is $a"
done
并打印:
the new value is 24
the new value is 25
the new value is 26
the new value is 27
the new value is 28
the new value is 29
the new value is 30
the new value is 31
the new value is 32
the new value is 33
the new value is 34
the new value is 35
the new value is 36
the new value is 37
the new value is 38
the new value is 39
the new value is 40
the new value is 41
the new value is 42
the new value is 43
the new value is 44
the new value is 45
答案 1 :(得分:0)