我编写了一个简单的脚本,将用户在地球上的体重转换为在月球上的体重。然而,在程序结束时,我试图询问用户是否想重复该过程并阅读他们的响应。
如果用户给予肯定回答,则脚本应重复,否则脚本应退出。
这是我到目前为止所拥有的,但是如果用户决定不退出,我无法弄清楚如何让脚本重复。
echo -n "Enter starting weight: "
read star
echo -n "Enter ending weight: "
read end
echo -n "Enter weight increment: "
read increment
while [ $star -le $end ]
do
moonweight=`echo $star \* .166 | bc`
echo "$star pounds on earth = $moonweight pounds on the moon"
star=`expr $star + $increment`
done
notDone=true
while [ $notDone ]
do
echo -n "Enter a number or Q to quit: "
read var1 junk
var1=`echo $var1 | tr 'A-Z' 'a-z'`
if [ $var1 = "q" ]
then
echo "Goodbye"
exit
else
fi
done
答案1
将其包装到另一个 while 循环中:
while :
do
echo -n "Enter starting weight: "
read star
echo -n "Enter ending weight: "
read end
echo -n "Enter weight increment: "
read increment
while [ "$star" -le "$end" ]
do
moonweight=`echo $star \* .166 | bc`
echo "$star pounds on earth = $moonweight pounds on the moon"
star=`expr $star + $increment`
done
notDone=true
while $notDone
do
echo -n "Enter a number or Q to quit: "
read var1 junk
var1=`echo $var1 | tr 'A-Z' 'a-z'`
if [ "$var1" = "q" ]
then
echo "Goodbye"
exit
else
notDone=false
fi
done
done