【发布时间】:2011-04-12 20:33:09
【问题描述】:
我对 linux 非常陌生,我编写了一个简单的 bash shell 脚本,它要求用户输入一个数字,然后要求另一个数字并显示数字的总和和乘积。我对此没有任何问题,但我想循环脚本。
例如,我想询问用户是否要退出,如果他们选择不退出,脚本会重新开始并再次询问两个数字。如果有谁知道循环的东西,你能帮我吗?谢谢。
这是我的代码:
#!/bin/bash
echo -n "Name please? "
read name
echo "enter a number."
read number1
echo "enter another number"
read number2
echo "Thank you $name"
let i=0
let i=$number1+$number2
let x=0
let x=$number1*$number2
echo "The sum of the two numbers is: $i"
echo "The product of the two numbers is: $x"
echo "Would you like to quit? Y/N? "
quit=N
while [ "$quit" = "Y" ]
do
clear
while ["$quit" != "Y" ]
do
echo "enter a number."
read number1
echo "enter another number"
read number2
echo "Thank you $name"
let i=0
let i=$number1+$number2
let x=0
let x=$number1*$number2
echo "The sum of the two numbers is: $i"
echo "The product of the two numbers is: $x"
echo "Would you like to quit? Y/N? "
【问题讨论】:
-
投票关闭它,因为它是面向外壳的,可能更适合 Superuser.com
-
来吧,bash 编程比 php 少吗? ;-)