Case Branch Selection Structure:
Case word in [mode [| mode] ...) command;;] ... esac
Case variable reference in
Mode 1)
Branch 1
;;
Mode 2)
Branch 2
;;
...
*)
Default Branch
;;
Esac
Mode (pattern):
1. Normal text characters
2.globbing-style wildcard characters:
*: Any character of any length
?: any single character
[]: Any single character within the range
[^]: Any single character outside the range
3.|: Or
Write a script:
Prompt the user to enter information, and then determine whether the information entered by the user is legitimate;
#!/bin/bash
#
Read-p "Please make your choice[yes of No]:" Choice
Case $CHOICE in
Yes
echo "right."
;;
No
echo "wrong."
;;
*)
echo "Unknown."
;;
Esac
if CONDITION1; Then
STATEMENT
Elif CONDITION2; Then
STATEMENT
Elif CONDITION3; Then
STATEMENT
...
Else
STATEMENT
Fi
The difference between the multi-branch structure of if and the branching structure of a case:
Same point:
1. All the conditions are true, the execution of the corresponding branch of the statement, the condition is false, do not execute;
2. You can set the default branch statement, that is: when all conditions do not match, the execution of the statement;
Different points:
1.if is judged correctly by the return value of the execution state of the command, and the case is judged correctly based on whether the value of the variable is a matching pattern;
Each branch of 2.case must use ';; ' End
Script for managing user accounts, Fourth edition: using Case statements to implement
#!/bin/bash
#
If [$#-lt 2]; Then
echo "Usage: $ (basename $)-a user1,user2,..., UserN | -D user1,user2,..., UserN. "
Exit 5
Fi
Case $ in
-a)
For I in $ (echo $ | tr ', '); Do
If ID $I &>/dev/null; Then
echo "$I exists already."
Else
Useradd $I
echo $I | passwd--stdin $I &>/dev/null
echo "Create $I successfully."
Fi
Done
;;
-D)
For J in $ (echo $ | tr ', '); Do
If ID $J &>/dev/null; Then
Userdel-r $J
echo "Delte $J finished."
Else
echo "User $J does not exist."
Fi
Done
;;
*)
echo "Usage: $ (basename $)-a user1,user2,..., UserN | -D user1,user2,..., UserN. "
Exit 6
Esac
While
While command; Do command; Done
While CONDITION; Do
Loop body
Done
Enter the cycle condition: condition has been true;
Exit cycle Condition: condition is false;
Until
until order; Do command; Done
Until CONDITION; Do
Loop body
Done
Enter the cycle condition: condition has been false;
Exit cycle Condition: condition is true;
While CONDITION; Do CMD; Done
Equivalent
Until! CONDITION; Do CMD; Done
Note: For the while and until two loop structure, if you want to implement the variable increment operation, it must be given manually;
Using the while and until loop structure, calculate the number of all integers within 100;
#!/bin/bash
#
Declare-i I=1
While [$I-le 100]; Do
Let sum+= $I
Let i++
Done
Echo $SUM
#!/bin/bash
#
Declare-i I=1
Until [$I-GT 100]; Do
Let sum+= $I
Let i++
Done
Echo $SUM
Loop Control statement:
Continue
Break
Continue
Continue [n]
End of the first nth layer of this cycle, directly into the next round of conditions to judge, if the conditions of the circulation, the next round to open the cycle;
Break
Break [n]
Advance technology n-tier cycle; no further cycles;
Infinite loop usage:
While true; Do
Loop body
Done
until false; Do
Loop body
Done
In such a circular structure, continue and break must be used appropriately to ensure that the cycle does not persist;
A while loop and a until loop that can implement traversal functions;
while read LINES; Do
Loop body
Done </path/from/somefile
Until! Read LINES; Do
Loop body
Done </path/from/somefile
Select
The Select loop is used primarily to create a menu-style list for users to select;
The list is arranged in numerical order, we just select the number;
In general, select is used with case;
Select is an infinite loop structure, so you must use the break command in the loop body to exit the loop, or you can use the Exit command to terminate the script directly;
Select NAME [in Word ...;] do command; Done
Select NAME [in LIST]; Do
Command
Done
To define a function:
A function is made up of two parts:
Function name + function body (Shell statement block capable of implementing standalone functions)
Syntax One:
function Func_name {
function body
}
Syntax Two:
Func_name () {
function body
}
Note: The function name and () can not be added white space characters;
Note: Functions can be defined in an interactive environment or in a script.
Use of functions
When the function is defined, all the commands contained in its function body are not executed, and the command statements are executed only when the function is called;
Invocation method: Called by giving the function name directly;
There are many functions that are stored in a file that is dedicated to saving functions, and if you want to invoke a function saved in such a file, use the Source command (.). Load the file, and then call the function in a way that directly gives the function name;
Use the SET command to view all functions in effect in the current shell;
Use the unset command to undo a function that has already been defined;
The return value of the function:
Two kinds of return values:
Return value of the execution result of the function:
1. The result of the Echo or printf command output is used in the function body;
2. The result of some command output in the function body;
The status return value of the function:
1. The execution status return value of the last command in the function;
2. Custom Exit Status code:
return [n]
n:0-255 (1 2 127 not used as much as possible)
0: No error return
1-255: Return with Error
Note: As long as the function executes, the return command is encountered, regardless of whether the command statement in the function is complete or not, immediately exits the function;
The life cycle of a function:
Starting from being called, until the execution of a return command or all statements is completed;
Arguments to functions
In the function body, you can use $1,$2,.. Positional variables provide parameters for functions, you can also reference all positional parameters using $* or [email protected], and you can use $ #计算为函数传递的参数个数;
When calling a function, separate multiple arguments directly after the function name with whitespace characters, for example: Func_name arg1 arg2 ...
The positional parameter passed to the function parameter is the string sequence separated by a blank character after the function name when the function is called, and is not the same as the location parameter of the script;
Variable:
A variable in the shell is a weak variable
1. Without prior notice
2. No need to specify variable type, default to character type
Variable classification:
Environment variables:
Current Shell and child shell
Local variables:
Current Shell
Local variables:
Local Var_name=value
Current function body
Positional variables
Special variables
Recommendation: Manually revoke any variables that you have defined or declared;
recursive invocation of functions
In simple terms, the function itself is called in the function body;
Factorial:
n!=n* (N-1)!=n* (N-1) * (N-2)!=...=n* (N-1) * (N-2) *...*2*1
Shell code:
#!/bin/bash
# Author:Tianyu.Zhao
#
Fact () {
If [$1-eq 0] | | [$1-eq 1]; Then
Echo 1
Else
echo "$[$1*$ (Fact $[$1-1])]"
Fi
}
Echo-n "$1!="
Fact $
Linux Learning Notes: looping, defining functions