Shell Script Loop statement--select loop

Source: Internet
Author: User

Select loop
Mainly used to create menus, the menu items arranged numerically are displayed on the standard output, and the PS3 indicator is displayed, waiting for the user to enter a number from the user input menu list, execute the corresponding command,
User input is saved in the built-in variable REPLY
Select is often used in conjunction with case with a for loop, you can omit the in list, at which point the position variable is used
Select is an infinite loop, you must manually specify an exit condition if you want to exit, typically the exit condition is specified in case

Syntax format:
Select variable in list
Do
Circular Body Command
Done

Select Loop Application Example:

Use Select to create a selection menu, 1, add user, 2, delete user, 3, add Group; 4, delete group; 5, exit

#!/bin/bash

#Author: Wangjun
#Contact qq:183530300
#Version: 1.0
#Create time:2016-08-18 19:29:03
#Description: Select test

ps3= "Please choice:"
Select Choose in "Add User" "Delet user" "Add Group" "Delet Group" "Exit"
Do
Case $choose in
"Add User")
Read-p "Please input a new username:" User
! ID $user &>/dev/null && useradd $user &>/dev/null && echo "$user user add Success" | | echo "$user User exists"
;;
"Delet user")
Read-p "Please input a exists username:" User
ID $user &>/dev/null && userdel-r $user &>/dev/null && echo "$user user delet success" | | echo "$user user doesn ' t exist"
;;
"Add Group")
Read-p "Please input a new groupname:" Group
! (Getent Group | grep "^ $group \>") &>/dev/null && groupadd $group &>/dev/null && echo "$ Group group Add Success "| | echo "$group Group exists"
;;
"Delet Group")
Read-p "Please input a exists groupname:" Group
Getent Group | grep "^ $group \>" &>/dev/null && groupdel $group &>/dev/null && echo "$group group Delet Success "| | echo "$group group doesn ' t exist"
;;
Exit
Exit
;;
Esac
Done

This article is from the "Love Firewall" blog, be sure to keep this source http://183530300.blog.51cto.com/894387/1840670

Shell Script Loop statement--select loop

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.