用shell编程,实现累加

来源:百度知道 编辑:UC知道 时间:2024/06/28 02:43:45
如果用shell编写程序, 要求逐个输入数字,由1+2+3....一直累加到输入数字0为止 请问怎么编吖 ?
不是自动算出来吖...是手动输入
//也拜托别贴一堆无用的文章上来吖 谢谢

#脚本开始
#!/bin/sh
b=0
echo -n 'input a number:'
read a
while [ $a -ne 0 ]
do
b=`expr $a + $b`
echo -n 'input other number:'
read a
done
echo the plus is $b
#脚本结束

linux 上测试通过

就用个until循环就行了 read一下, 判断如果是0就退出呗

echo -n "Enter a number:"
read a
sum=0
until [ $a -eq 0 ]
do
let sum=sum+$a
echo -n "Enter a number:"
read a
done
echo "The result is: $sum"