我想按照序列将每个条件内的变量值附加到数组中,即这里首先是60s,然后是39.8N,最后是59.8S。我想添加元素 根据我的代码,只有最后4个元素被追加到数组中。
#!/bin/bash
for i in $(seq -60 1 30);do
nextint=$((i+1))
sum=$((i+nextint))
mid=`echo "$sum / 2" | bc -l`
sum=`echo $i + $mid | bc`
topmid=`echo "$sum / 2" | bc -l`
sum=`echo $mid + $nextint | bc`
bottommid=`echo "$sum / 2" | bc -l`
k=0.0
#printf "$i%.*f\n$topmid%.*f\n$mid%.*f\n$bottommid%.*f\n"
# "$topmid $mid $bottommid"
#printf "%.*f\n" $i $topmid $mid $botttommid
n='N'
s='S'
array=()
if (( $(echo "$i == $k" | bc -l) )); then
i=$( printf "%.f\n" "$i")
topmid=$(printf "%.1f\n" "$topmid")
mid=$(printf "%.1f\n" "$mid")
bottommid=$( printf "%.1f\n" "$bottommid")
topmid=$topmid$n
mid=$mid$n
bottommid=$bottommid$n
echo "$i"
echo "$topmid"
echo "$mid"
echo "$bottommid"
array+=("$i")
array+=("$topmid")
array+=("$mid")
array+=("$bottommid")
# echo "${array[@]}"
#fi
#done
elif (( $( echo "$i > $k" |bc -l ) ))
#n='N'
then
i=$( printf "%.f\n" "$i")
topmid=$(printf "%.1f\n" "$topmid")
mid=$(printf "%.1f\n" "$mid")
bottommid=$( printf "%.1f\n" "$bottommid")
i=$i$n
topmid=$topmid$n
mid=$mid$n
bottommid=$bottommid$n
echo "$i"
# eco "${array[@]}"
echo "$topmid"
echo "$mid"
echo "$bottommid"
array+=("$i")
array+=("$topmid")
array+=("$mid")
array+=("$bottommid")
else
#s='S'
<<com
printf "%.f\n" "$i"
printf "%.1f\n" "$topmid"
printf "%.1f\n" "$mid"
printf "%.1f\n" "$bottommid"
com
i=$( printf "%.f\n" "$i")
topmid=$(printf "%.1f\n" "$topmid")
mid=$(printf "%.1f\n" "$mid")
bottommid=$( printf "%.1f\n" "$bottommid")
i=$(echo $i$s| sed 's/-//g')
topmid=$(echo $topmid$s | sed 's/-//g')
mid=$(echo $mid$s | sed 's/-//g')
bottommid=$(echo $bottommid$s | sed 's/-//g')
echo "$i"
echo "$topmid"
echo "$mid"
echo "$bottommid"
array+=(" $i")
array+=(" $topmid")
array+=(" $mid")
array+=("$bottommid")
fi
done
echo "${array[*]}"
echo "${#array[@]}"
输出为- 60S 59.8秒 59.5秒 59.2秒 59S 58.8秒 58.5秒 58.2秒 58S ..... 28.8牛 29N 29.2牛 29.5牛 29.8牛 30N 30.2牛 30.5牛 30.8牛 30N 30.2N 30.5N 30.8N 4
我希望将整个变量值数组作为序列60N 59.8N ....... 30N 30.2N 30.5N 30.8N
TIA。** 我想将整个变量值数组作为序列中的元素60N 59.8N ....... 30N 30.2N 30.5N 30.8N **
答案 0 :(得分:0)
将array=()
移动到循环之外:
#!/bin/bash
array=()
for i in $(seq -60 1 30);do
nextint=$((i+1))
sum=$((i+nextint))
mid=`echo "$sum / 2" | bc -l`
sum=`echo $i + $mid | bc`
topmid=`echo "$sum / 2" | bc -l`
sum=`echo $mid + $nextint | bc`
bottommid=`echo "$sum / 2" | bc -l`
k=0.0
#printf "$i%.*f\n$topmid%.*f\n$mid%.*f\n$bottommid%.*f\n"
# "$topmid $mid $bottommid"
#printf "%.*f\n" $i $topmid $mid $botttommid
n='N'
s='S'
if (( $(echo "$i == $k" | bc -l) )); then
i=$( printf "%.f\n" "$i")
topmid=$(printf "%.1f\n" "$topmid")
mid=$(printf "%.1f\n" "$mid")
bottommid=$( printf "%.1f\n" "$bottommid")
topmid=$topmid$n
mid=$mid$n
bottommid=$bottommid$n
echo "$i"
echo "$topmid"
echo "$mid"
echo "$bottommid"
array+=("$i")
array+=("$topmid")
array+=("$mid")
array+=("$bottommid")
# echo "${array[@]}"
#fi
#done
elif (( $( echo "$i > $k" |bc -l ) ))
#n='N'
then
i=$( printf "%.f\n" "$i")
topmid=$(printf "%.1f\n" "$topmid")
mid=$(printf "%.1f\n" "$mid")
bottommid=$( printf "%.1f\n" "$bottommid")
i=$i$n
topmid=$topmid$n
mid=$mid$n
bottommid=$bottommid$n
echo "$i"
# eco "${array[@]}"
echo "$topmid"
echo "$mid"
echo "$bottommid"
array+=("$i")
array+=("$topmid")
array+=("$mid")
array+=("$bottommid")
else
#s='S'
<<com
printf "%.f\n" "$i"
printf "%.1f\n" "$topmid"
printf "%.1f\n" "$mid"
printf "%.1f\n" "$bottommid"
com
i=$( printf "%.f\n" "$i")
topmid=$(printf "%.1f\n" "$topmid")
mid=$(printf "%.1f\n" "$mid")
bottommid=$( printf "%.1f\n" "$bottommid")
i=$(echo $i$s| sed 's/-//g')
topmid=$(echo $topmid$s | sed 's/-//g')
mid=$(echo $mid$s | sed 's/-//g')
bottommid=$(echo $bottommid$s | sed 's/-//g')
echo "$i"
echo "$topmid"
echo "$mid"
echo "$bottommid"
array+=(" $i")
array+=(" $topmid")
array+=(" $mid")
array+=("$bottommid")
fi
done
echo "${array[*]}"
echo "${#array[@]}"
有效地,您在循环的每一位都将其清空,并且最多设置4个值。 set -x
非常适合诊断这些类型的问题。
使用https://www.shellcheck.net/
检查您的代码。最佳实践等等。