Structure of the while, until, case, and select statements in bash
Case statement:
Use the case structure in the script:
Case $ {VAR_NAME} in
PATTERN1)
COMMAND
...
;;
PATTERN2)
COMMAND
...
;;
...
Esac
PATTERN can be the following types of characters:
1. common text characters
2. Globbing-style wildcard:
*: Any character of any length
? : Any single character;
[]: Any single character in the specified range;
[^]: Any single character out of the specified range;
3. |
Or character
Differences between the multi-branch structure of if and the branch structure selected by case:
Similarities:
1. When the judgment condition is true, the statement in the corresponding branch will be executed; if the condition is false, the statement will be skipped and not executed;
2. You can set the default branch statement, that is, the statement that will be executed only when all the given conditions are judged to be false;
Differences:
1. if statements in a branch are determined based on the true or false returned values of the command execution status;
Case is used to determine whether to execute a statement in a branch based on the true or false results of matching values saved in the variable with the specified mode;
2. if each branch does not need to end with a separate end tag, and each branch of case must end;
Write a script for managing user accounts. The fourth edition uses the case statement + for loop to simultaneously create and delete users;
#! /Bin/bash
#
# Helps function, displaying prompt information and help information
Helps (){
Echo-e "Usage: $ (basename $0) options... USERLIST \ n"
Echo-e "Options :"
Echo-e "-a, -- add: \ vAdd some users from USERLIST ."
Echo-e "-d, -- delete: \ vDelete some users from USERLIST ."
Echo-e "-h, -- help: \ vPrint help informationn ."
Echo-e "-v, -- verbose: \ vPrint more informationn about manage users ."
Echo
Echo-e "userlist format :"
Echo-e "USERNAME1, USERNAME2,..., USERNAMEN"
}
# Number of input options
If [$ #-lt 1]; then
Helps
Exit 5
Fi
ADDUSER = 0
DELUSER = 0
DEBUG = 0
# Filter and execute the options
For I in $ (seq $ #); do
If [$ #-ne 0]; then
Case $1 in
-H | -- help)
Helps
Exit 0
;;
-V | -- verbose)
DEBUG = 1
Shift
;;
-A | -- add)
ADDUSERLIST = $2
ADDUSER = 1
Shift 2
;;
-D | -- delete)
DELUSERLIST = $2
DELUSER = 1
Shift 2
;;
*)
Helps
Exit 6
;;
Esac
Fi
Done
# Add a user
If [$ ADDUSER-eq 1]; then
For J in $ (echo $ ADDUSERLIST | tr ', '''); do
If! Id $ J &>/dev/null; then
Useradd $ J &>/dev/null
Echo $ J | passwd -- stdin $ J &>/dev/null
[$ DEBUG-eq 1] & echo "Create user $ J successfully ."
Else
Echo "$ J exist already ."
Fi
Done
Fi
# Delete a user
If [$ DELUSER-eq 1]; then
For J in $ (echo $ DELUSERLIST | tr', '''); do
If id $ J &>/dev/null; then
Userdel-r $ J &>/dev/null
[$ DEBUG-eq 1] & echo "Delete user $ J finished ."
Else
Echo "$ J does not exist yet ."
Fi
Done
Fi
# Whether the option is "-a" or "-a", the add user operation is performed first, because the add user operation is executed in the front and the script content is executed in sequence.
While Loop Structure
While: while COMMANDS; do COMMANDS; done
The following structures can be written in the script:
While CONDITION; do
COMMANDS
Done
CONDITION for the while loop to enter the loop: The CONDITION logic determines that the result is true;
CONDITION for exit of while loop: The CONDITION logic determines that the result is false;
Until Loop Structure
Until: until COMMANDS; do COMMANDS; done
The following structures can be written in the script:
Until CONDITION; do
COMMANDS
Done
CONDITION for the until loop to enter the loop: The CONDITION logic determines that the result is false;
CONDITION for until loop Exit: The CONDITION logic determines that the result is true;
Note:
1. while CONDITION is equivalent to! CONDITION
2. In the while and until loop structures, there is no variable auto-increment or auto-increment change method. Therefore, you need to use the statement to manually give the variable change method;
Write a script and use the while or until loop to calculate the sum of integers less than 100;
#! /Bin/bash
#
# Exit the loop when $ I = 100. Otherwise, the system enters 101.
Declare-I I = 0
Until [$ I-eq 100]; do
Let I ++
Let SUM + = $ I
Done
Echo $ SUM
#! /Bin/bash
#
Declare-I I = 0
While [$ I-lt 100]; do
Let I ++
Let SUM + = $ I
Done
Echo $ SUM
Loop Control statement:
Continue
Continue: continue [n]
Resume for, while, or until loops.
Terminate the current loop at Layer n in advance and directly go to the next round of condition judgment. If the condition judgment result still meets the condition for entering the cycle, start the next cycle;
Break
Break: break [n]
Exit for, while, or until loops.
Terminate the n-layer loop ahead of schedule and stop subsequent cycles;
The usage of the while and until special loops is as follows:
1. Infinite Loop Method:
While true; do
COMMANDS
Done
Until false; do
COMMANDS
Done
Guess digital games:
#! /Bin/bash
#
NUMBER = $ [RANDOM % 100 + 1]
While true; do
Read-p "Input a number:" INPTNUM
If [$ INPTNUM-gt $ NUMBER]; then
Echo "Too big"
Elif [$ INPTNUM-lt $ NUMBER]; then
Echo "Too small"
Else
Echo "Yes! You WIN. That's $ NUMBER ."
# Exit when the input number matches the random number
Break
Fi
Done
#! /Bin/bash
#
NUMBER = $ [RANDOM % 100 + 1]
Until false; do
Read-p "Input a number:" INPTNUM
If [$ INPTNUM-gt $ NUMBER]; then
Echo "Too big"
Elif [$ INPTNUM-lt $ NUMBER]; then
Echo "Too small"
Else
Echo "Yes! You WIN. That's $ NUMBER ."
Break
Fi
Done
Note: In such a loop structure, you need to add a continue or break control statement to make the infinite loop controllable;
2. The while and until loop structures that implement the traversal function:
While read LINES; do
COMMANDS
Done </PATH/FROM/SOMEFILE
Until! Read LINES; do
COMMANDS
Done </PATH/FROM/SOMEFILE
Note: We recommend that you use for traversal;
Select Loop Structure
Select: select NAME [in WORDS...;] do COMMANDS; done
Select words from a list and execute commands.
The select loop is also a way to traverse the list to create a visual menu. Each list item has a number corresponding to it for the user to choose to use, and the user only needs to select its number;
Select is an infinite loop structure by default. Therefore, you must provide the exit condition for the select statement in the loop body. Generally, you can use the break or exit command;
Generally, the select loop is used together with the case, and a reasonable value has been determined;
The format implemented in the script:
Select VAR_NAME in LIST; do
COMMANDS
Done
Write a script to display the ID of the user whose/bin/bash is the default shell;
#! /Bin/bash
#
# Select User Name or quit button
Select I in $ (awk-F: '// bin \/bash $/{print $1}'/etc/passwd) quit; do
Case $ I in
Quit)
Exit
;;
*)
Echo "The UID of $ I is $ (id-u $ I )"
;;
Esac
Done
This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151446.htm