假设用户选择 5(antall)。然后我想打印 5+6+7+8+4 (瞬间)。在我的脚本中我得到这个打印:
5+6+7+8+4**+**
如何去掉最后一个+?? (我已经尝试在 for 循环中 echo“+”,但没有设法让我正确)
我的脚本:
plusse () {
while true; do
random=0
summit=0
read -p "Hvor mange tall vil du plusse>> " antall
echo "Hva er? "
for (( i=1; i<=$antall; i++ )); do
random=$(( RANDOM % 10 ))
summit=$(( summit + $random ))
echo -n "$random "
echo -n "+"
done
echo
read -p "Svaret mitt er>> " svarr
echo "$summit"
if [[ $svarr == $summit ]]; then
echo "Gratulerer! $svarr er riktig svar! "
read -p "Vil du fortsette?>> " fortsette
case $fortsette in
ja|j|JA|Ja|y|Yes) continue
;;
nei|n|Nei|n|N) break 1
;;
*) exit 1
;;
esac
else
echo "Beklager, $svarr er feil. Riktig svar er $summit"
read -p "Vil du fortsette?>> " fortsette
case $fortsette in
ja|j|JA|Ja|y|Yes) continue
;;
nei|n|Nei|n|N) break 1
;;
*) exit 1
;;
esac
fi
done
}
答案1
你的代码:
for (( i=1; i<=$antall; i++ )); do
random=$(( RANDOM % 10 ))
summit=$(( summit + $random ))
echo -n "$random "
echo -n "+"
done
将要总是+
在所选数字后添加 a 。您可能想做类似的事情:
prefix=""
for (( i=1; i<=$antall; i++ )); do
random=$(( RANDOM % 10 ))
summit=$(( summit + $random ))
echo -n "${prefix}${random}"
prefix="+"
done