Go Linux Shell Series Tutorial (14) Shell Select Tutorial

Source: Internet
Author: User


This article is part of the Linux Shell Series Tutorial series, which includes the following 18 parts:
  1. Linux Shell Series Tutorial (a) Shell introduction
  2. Linux Shell Series Tutorial (ii) the first shell script
  3. Linux Shell Series Tutorial (c) Shell variables
  4. Shell comments for the Linux Shell Series Tutorial (d)
  5. Linux Shell Series Tutorial (v) Shell string
  6. Linux Shell Series Tutorial (vi) Shell array
  7. Linux Shell Series Tutorial (vii) Shell output
  8. Linux Shell Series Tutorial (eight) Shell printf command details
  9. Linux Shell Series Tutorial (ix) Shell judging if else usage
  10. Linux Shell Series Tutorial (10) Shell for Loop
  11. Linux Shell Series Tutorial (11) Shell while loop
  12. Linux Shell Series Tutorial (12) Shell until loop
  13. Linux Shell Series Tutorial (13) Shell Branch statement case ... ESAC tutorial
  14. Linux Shell Series Tutorial (14) Shell Select Tutorial
  15. Linux Shell Series Tutorial (15) Introduction to Shell functions
  16. Linux Shell Series Tutorial (16) Shell input and output redirection
  17. The Linux Shell Series Tutorial (17) Shell file contains
  18. Linux Shell Series Tutorials Directory


For more information on the series, see the Linux Shell Series Tutorial:



Linux Shell Series tutorial, Welcome to join the Linux Technology Exchange Group: 479935456



Original: https://www.linuxdaxue.com/linux-shell-select-command.html



Select with case to use, you can complete a lot of complex menu control options.



Select, unlike other flow controls, does not have a similar statement in a programming language such as C, and today introduces the use of the shell SELECT statement.


First, Shell SELECT statement syntax


The syntax for the SELECT statement in the shell is as follows:


select name   [in   list ] 
do 
    statements that can use  $name... 
done


Note: Select first produces the menu options in the list, and then executes the statements between the do...done below. The menu item that the user selects is saved in the $name variable.



Additionally: The SELECT command uses the PS3 prompt, which defaults to (#?). ;



In select Use, you can set the hint string with ps3= ' string '.


Ii. Examples of Shell SELECT statements


As usual, learn the use of Shell Select by example:


#! / bin / bash
#Author: linuxdaxue.com
#Date: 2016-05-30
#Desc: Shell select exercise
PS3 = ‘Please choose your number:‘ # Set the prompt string.
echo
select number in "one" "two" "three" "four" "five"
do
echo
echo "Your choose is $ number."
echo
break
done
exit 0


Description: The above example presents a menu for the user to choose from and then displays the menu item selected by the user.



This is one of the most basic examples, showing the basic usage of select. Of course, you can also remove the break and let the program go through the loop.



Here is the output after removing the break:


$./select.sh
1) one
2) two
3) three
4) four
5) five
Please choose your number: 1

Your choose is one.

Please choose your number: 2

Your choose is two.

Please choose your number: 3

Your choose is three.

Please choose your number: 4

Your choose is four.

Please choose your number: 5

Your choose is five.


Then we change the example slightly, adding the CASE...ESAC statement:


#! / bin / bash
#Author: linuxdaxue.com
#Date: 2016-05-30
#Desc: Shell select case exercise
PS3 = ‘Please choose your number:‘ # Set the prompt string.
echo
select number in "one" "two" "three" "four" "five"
do
case $ number in
one)
echo Hello one!
;;
two)
echo Hello two!
;;
*)
echo
echo "Your choose is $ number."
echo
;;
esac
#break
done
exit 0


That way, case will process each of the user's options and then execute the corresponding statement. The output is as follows:


$./select2.sh
1) one
2) two
3) three
4) four
5) five
Please choose your number: 1
Hello one!
Please choose your number: 2
Hello two!
Please choose your number: 3

Your choose is three.

Please choose your number: 4

Your choose is four.


By modifying these statements, you can write very complex scripts. How, is not very strong, quickly try it!



For more Linux shell tutorials see: Linux Shell Series Tutorials



Go Linux Shell Series Tutorial (14) Shell Select Tutorial


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.