【发布时间】:2019-06-20 09:43:45
【问题描述】:
我有一个简单的代码来基于简单的算术等式在循环中分配变量
# assign initial value
restr_start='25'
# assign a new variable, which is a number that will decrease initial value by 5
# keeping always the value of previous variable as restr_prev
for step in {1..4}; do
let "restr=(${restr_start} - (5 * ${step}))"
let "restr_prev=(${restr} + (5 * ${step}))"
echo this is $restr current restart
echo this is $restr_prev previous restart
done
从这个脚本中我期望得到:
this is 20 current restart
this is 25 previous restart
this is 15 current restart
this is 20 previous restart
this is 10 current restart
this is 15 previous restart
this is 5 current restart
this is 10 previous restart
然而我实际上有什么
this is 20 current restart
this is 25 previous restart
this is 15 current restart
this is 25 previous restart
this is 10 current restart
this is 25 previous restart
this is 5 current restart
this is 25 previous restart
为什么 $restr_prev 通常是不变的?我如何修改代码,例如使用某些东西代替 let?
【问题讨论】:
-
数学看起来正确!你有什么问题?
-
@Inian 不,
restr_prev的公式不正确。请查看我的答案以获得更准确的解释。 -
@EduardItrich:我的意思是,对于代码,OP 产生的输出是正确的。对于不同的输出,需要更改代码
-
不要使用
let;在存在 POSIX 算术表达式的情况下,它已经过时了。restr=$((restr_start - 5*step)). -
或者更好(但不是 POSIX):
(( restr = restr_start - 5 * step ))- 请注意,在这两个示例中,变量名称中都省略了美元符号和花括号。
标签: bash loops variables equation