The Bash Shell also offer select Loop, the syntax is:
select varName in list do command1 command2 .... ...... commandN done
OR (combine both select and case statement)
select varName in list do case $varName in pattern1) command1;; pattern2) command2;; pattern1) command3;; *) echo "Error select option 1..3";; esac done
Create a shell script called select.sh:
#!/bin/bash
# Set PS3 prompt
PS3="Enter the space shuttle to get more information : "
# set shuttle list
select shuttle in columbia endeavour challenger discovery atlantis enterprise pathfinder
do
echo "$shuttle selected"
done
Save and close the file. Run it as follows:
chmod +x select.sh
./select.sh
Sample outputs:
/tmp/x.sh 1) columbia 3) challenger 5) atlantis 7) pathfinder 2) endeavour 4) discovery 6) enterprise Enter the space shuttle name to get more information : 1 columbia selected Enter the space shuttle name to get more information :
Another select loop example and decision making does with case..in..esac statement (selectshuttle.sh):
#!/bin/bash
# The default value for PS3 is set to #?.
# Change it i.e. Set PS3 prompt
PS3="Enter the space shuttle to get quick information : "
# set shuttle list
select shuttle in columbia endeavour challenger discovery atlantis enterprise pathfinder
do
case $shuttle in
columbia)
echo "--------------"
echo "Space Shuttle Columbia was the first spaceworthy space shuttle in NASA‘s orbital fleet."
echo "--------------"
;;
endeavour)
echo "--------------"
echo "Space Shuttle Endeavour is one of three currently operational orbiters in the Space Shuttle."
echo "--------------"
;;
challenger)
echo "--------------"
echo "Space Shuttle Challenger was NASA‘s second Space Shuttle orbiter to be put into service."
echo "--------------"
;;
discovery)
echo "--------------"
echo "Discovery became the third operational orbiter, and is now the oldest one in service."
echo "--------------"
;;
atlantis)
echo "--------------"
echo "Atlantis was the fourth operational shuttle built."
echo "--------------"
;;
enterprise)
echo "--------------"
echo "Space Shuttle Enterprise was the first Space Shuttle orbiter."
echo "--------------"
;;
pathfinder)
echo "--------------"
echo "Space Shuttle Orbiter Pathfinder is a Space Shuttle simulator made of steel and wood."
echo "--------------"
;;
*)
echo "Error: Please try again (select 1..7)!"
;;
esac
done
Save and close the file. Run it as follows:
chmod +x selectshuttle.sh
./selectshuttle.sh
Sample outputs:
原文:http://www.cnblogs.com/zhaoxinshanwei/p/6005682.html