Exit select loop

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← select loopHomeBreak statement →

You can exiting the select loop statement either pressing Ctrl+C or by adding the exit option as follows:

#!/bin/bash
# Set PS3 prompt
PS3="Enter the space shuttle to get quick information : "
 
# set shuttle list
# exit option 
select shuttle in columbia endeavour challenger discovery atlantis enterprise pathfinder exit
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 "--------------"							
			;;
                exit)
        	        echo "Bye!"
                        break
                        ;;
		*)		
			echo "Error: Please try again (select 1..8)!"
			;;		
	esac
done

Sample output:

1) columbia    3) challenger  5) atlantis    7) pathfinder
2) endeavour   4) discovery   6) enterprise  8) exit
Enter the space shuttle to get quick information : 5
--------------
Atlantis was the fourth operational shuttle built.
--------------
Enter the space shuttle to get quick information : 8
Bye!
← select loopHomeBreak statement →