The Select command in the Bash shell simply uses the example _linux shell

Source: Internet
Author: User

Objective
today I just wrote an automated packaging script, again using the bash shell, a good feeling of happiness. The main point here is to introduce the Select command, which can help us complete the menu selection function.

Format
I am also the first to use Select Process Control today, in PHP, Java, C these languages do not implement the Select function. In the Bash shell, the Select format is as follows:

  Select $var in ${list[@]} 
  do 
    statements so can use $var 
  done 

When select executes, the selection menu is given according to the list array, the result of the user selection is saved in the $var variable, and then the statements statement is executed. Once the execution is complete, give the menu again, waiting for the user to choose. If the user wants to jump out of the selection loop, you need to increase the break statement according to the condition in the loop body.

Example
Given a select example, you can refer to:

  #!/bin/bash 
   
  fruits= ( 
    "apple" 
    "pear" 
    "orange" 
    "Watermelon" 
  ) 
   
  echo "Please guess Which fruit I like: ' 
  select var in ${fruits[@]} ' do 
    if [$var = ' Apple ']; Then 
      echo "Congratulations, your are my good firend!" 
      Break 
    Else 
      echo "Try again!" 
    Fi done 
   

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.