Get the App
SLTechnology News&Howtos  ›  Servers  › 

Carding the basic knowledge of shell script & lt; eight & gt;: process Control select

Shulou Source: shulou.com Published: 2022-06-02 07:11:34 09月30日 Update

Select variable in list

Flag for the start of the do # loop

Every time a commands # loop variable takes a value, the loop body executes once

Flag for the end of the done # loop

The select loop is mainly used to create menus, and menu items in numerical order will be displayed on standard error, waiting for user input

The separator of the menu item is determined by the environment variable IFS

The prompt information used to guide the user to enter is stored in the environment variable PS3

If the user enters enter directly, the menu will be redisplayed

Similar to the for loop, omitting in list is equivalent to in "$*"

The user enters a number in the menu list and executes the corresponding command

The user's input is saved in the built-in variable REPLY.

Example 1

#! / bin/bash

#

# IFS is a system delimiter variable; if the input parameter variable is not specified, the system stores the parameters followed by the script in the REPLY variable by default

Clear

PS3= "What is your preferred OS?"

IFS=' |'

OS= "Linux | Gnu Hurd | FreeBSD | Mac OS X"

Select s in $OS

Do

Case $REPLY in

1 | 2 | 3 | 4) echo "You selected $s"

*) exit

Esac

Done

Execution result

1) Linux

2) Gnu Hurd

3) FreeBSD

4) Mac OS X

What is your preferred OS?1

You selected Linux

What is your preferred OS?4

You selected Mac OS X

What is your preferred OS?e

[root@localhost shell] #

Example 2

#! / bin/bash

#

# PS3 user-defined prompts

Clear

PS3= "What is your preferred scripting language?"

Select s in bash perl python ruby quit

Do

Case $s in

Bash | perl | python | ruby)

Echo "You selected $s"

Quit) exit

*) echo "You selected error,retry"

Esac

Execution result

1) bash

2) perl

3) python

4) ruby

5) quit

What is your preferred scripting language?1

You selected bash

What is your preferred scripting language?2

You selected perl

What is your preferred scripting language?5

[root@localhost shell] #

Example 3

#! / bin/bash

#

# Command package View script

PS3= "Select a program you want to execute:"

TOPLIST= "wget telnet htop atop nettop iftop ftop"

Clear

Select prog in $TOPLIST quit

Do

[[$prog = = quit]] & & exit

Rpm-Q $prog > / dev/null & & echo "$prog installed" | | echo "$prog is not installed"

Done

Execution result

1) wget 3) htop 5) nettop 7) ftop

2) telnet 4) atop 6) iftop 8) quit

Select a program you want to execute: 1

Wget installed

Select a program you want to execute: 8

[root@localhost shell] #

Tags: Variables inputs users loops menus instances results scripts information parameters commands numbers flags environments systems prompts delimiters followed loop bodies standards Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information Apple NVidia Redmi