Application of select in shell scripts

Source: Internet
Author: User

Select is also a kind of loop, it is more suitable for use in the case of user choice.

For example, we have one such requirement, after running the script, let the user go to select the number and then execute the command!

#!/bin/bashselect Mysql_v in 5.1 5.6docase $mysql _v in5.1) echo "You choose the 5.1" #break /break This line has been commented out;; 5.6) echo "You choose the 5.6" #break/break This line is commented out;; Esacdone

Then execute this script

[[Email protected] sbin]# sh 3.sh1) 5.12) 5.6#? 1you Choose the 5.1#? 2you Choose the 5.6#? 1you Choose the 5.1#? 1you Choose the 5.1#? 2you Choose the 5.6#?              2you Choose the 5.6#? /will find that as long as the choice will continue to carry out, and will not quit!

Add a break line to the script:

#!/bin/bashselect Mysql_v in 5.1 5.6docase $mysql _v in5.1) echo "You choose the 5.1" Break/bre AK was not annotated;; 5.6) echo "You choose the 5.6" break/break not commented;; Esacdone

Execute this script again: you will find that each time you choose, the execution will exit, and select is actually a loop, and the function of break is to exit the loop!

[[email protected] sbin]# sh 3.sh1)  5.12)   5.6#? 1YOU CHOOSE THE 5.1[[EMAIL PROTECTED] SBIN]# SH 3.SH1)  5.12 )  5.6#? 2                                        /Choice is the one in front "#? "Symbol can be modified, can be modified with PS3, the following script  you choose the 5.6 
[Email protected] sbin]# vim 3.sh#!/bin/bashps3= "Please select a number:"/Add this line in the script and run again! Select Mysql_v in 5.1 5.6docase $mysql _v in5.1) echo ' You choose the 5.1 ' break;; 5.6) echo "You choose the 5.6" break;; Esacdone Run Result: [[email protected] sbin]# sh 3.sh1) 5.12 5.6Please Select a number:1you choose the 5.1


This article is from the "Custom" blog, so be sure to keep this source http://zidingyi.blog.51cto.com/10735263/1724899

Application of select in shell scripts

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.