初学控制语句,练练手,于初学者共勉
#!/bin/bash
clear
cat<<EOF1
hello ! everybody who likes LINUX shell
here's a mini script test for LINUX shell as follows:
the first, enter two numbers (for us to see if they match our needs, as well),
and then choose one action from ADDING, SUBTRACTION
MULTIPLICATION and DIVISION. and finally you'll see the result
thank you for cooperation !
EOF1
sleep 2
cat<<EOF2
if you want to view the shell script, enter "y"
(press SPACE to next page, PAGEUP to previous page, PAGEDOWN to next page
ARROWs to more or less lines, "q" to quit "view")
otherwise, enter any other keys, then press ENTER
EOF2
read look
if [ $look == y -o $look == Y ]
then
clear
less $0
else
echo "let's go ahead !"
fi
sleep 2
echo "please give first number, then press ENTER"
read num1
echo "please give second number, then press ENTER"
read num2
((sum=$num1+$num2))
echo "the numbers you just put in are:"
for num in $num1 $num2
do
echo $num
done
sleep 3
while [ $sum -le 1000 ]
do
clear
cat<<EOF3
the numbers you gave doesn't match our needs
you haven't passed obstacle 1
we have to start it again!
EOF3
sleep 10
$0
done
cat<<EOF4
congradulations! you've passed obstacle 1
let's go on
EOF4
sleep 2
until [ $sum -ge 10000 ]
do
clear
cat<<EOF5
the numbers you gave still doesn't match our needs
you haven't passed obstacle 2
we have to start it again!
EOF5
sleep 10
$0
done
cat<<EOF6
congradulations! you've passed obstacle 2"
let's go on"
if you choose ADDING, please enter "a", then press ENTER
if you choose SUBTRACTION, please enter "s", then press ENTER
if you choose MULTIPLICATION, please enter "m", then press ENTER
if you choose DIVISION, please enter "d", then press ENTER
if you want to exit, please enter any other keys, then press ENTER
EOF6
read action
case $action in
a|A)
echo "working ..."
sleep 2
echo $[$num1+$num2]
echo done
;;
s|S)
echo "working ..."
sleep 2
echo $[$num1-$num2]
echo done
;;
m|M)
echo "working ..."
sleep 2
echo $[$num1*$num2]
echo done
;;
d|D)
echo "working ..."
sleep 2
echo $[$num1/$num2]
echo done
;;
*)
cat<<EOF7
if you're sure to quit, enter "y"
if you want to view the shell script, enter "v"
(press SPACE to next page, PAGEUP to previous page, PAGEDOWN to next page
ARROWs to more or less lines, "q" to quit "view")
if you want to romove the shell script, enter "r"
if you want to rerun the shell, enter any other keys
EOF7
read quit
if [ $quit == y -o $quit == Y ]
then
exit
elif [ $quit == v -o $quit == V ]
then
less $0
elif [ $quit == r -o $quit == R ]
then
rm -f $0
else
clear
cat<<EOF8
START FROM THE SCRATCH !
EOF8
sleep 2
clear
$0
fi
;;
esac