我是bash脚本编程的初学者。我用Python编写了一个小脚本,用于计算从初始值到最后一个值(以0.5递增)的列表值。Python脚本是:
# A script for grid corner calculation.
#The goal of the script is to figure out the longitude values which increase with 0.5
# The first and the last longitude values according to CDO sinfo
L1 = -179.85
L2 = 179.979
# The list of the longitude values
n = []
while (L1 < L2):
L1 = L1 + 0.5
n.append(L1)
print "The longitude values are:", n
print "The number of longitude values:", len(n)
我想通过bash shell创建一个相同的脚本。我尝试了以下方法:
!#/bin/bash
L1=-180
L2=180
field=()
while [ $L1 -lt $L2 ]
do
scale=2
L1=$L1+0.5 | bc
field+=("$L1")
done
echo ${#field[@]}
但它不起作用。有人可以告诉我我做错了什么吗?如果有人帮助我,我将不胜感激。
您没有将值正确分配给L1
。另外,-lt
期望整数,因此比较将在第一次迭代后失败。
while [ "$(echo "$L1 < $L2" | bc)" = 1 ]; do
do
L1=$(echo "scale=2; $L1+0.5" | bc)
field+=("$L1")
done
如果seq
有的话,您可以改用它,例如,
field=( $(seq -f %.2f -180 0.5 179.5) )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句