Syntax format for if structure
Single branch structure
If < conditional expressions > then Directives fiif < conditional expressions >; Then command fi
Dual-branch structure
If < conditional expressions >; Then instruction 1else instruction 2fi
Multi-branch structure
If < conditional expressions >; Then instruction 1elif instruction 2elif instruction 3fi
Functions in the shell
A Shell function is a block of code in a shell script that consists of a set of commands and statements that can be called by other scripts or other parts of the script, so the Shell function allows the program to be modularized, separating the code into separate task blocks, so that You do not have to rewrite the code every time to perform the same task.
Defines the syntax for a function:
Function_name () {commands ... #函数体, the command line executed in the function [return int;] #返回参数, if there is no return statement, the result of the last command of the function as the return value}function functions _name () {commands ... #函数体, the command line executed in the function [return int;] #返回参数, if there is no return statement, the result of the last command of the function as the return value}
When the function is called;
Defining functions and function bodies must be defined before the execution function name, and the execution of the shell is performed from top to bottom.
Execution method with parametric function:
Function Name parameter 1 parameter 2
Positional parameters in the function body ($, $, $#, $*, [email protected]) can be parameters of a function
The parameters of the parent script are temporarily obscured or hidden by the function arguments
$ more special, still the name of the parent script
When the function is complete, the original command-line arguments are restored
In the Shell function, the return command functions and works in the same way as exit, for jumping out of a function
Using exit inside the Shell function body terminates the entire shell script
The return statement returns an exit value to the calling program
Case statement
Case ' variable ' in value 1) Directive 1 ...; Value 2) instruction 2 ...; ... *) directive n;; Esac
Looping statements
(1) For loop
for variable in list; Do ... done
List Generation Method:
Direct use of the list;
Build list with file name wildcard
Use the {} or SEQ command to generate a number sequence using command generation
(2) Cycle
For ((EXPR1;EXPR2;EXPR3)); Do ... done
(3) While loop: for scenes with unknown cycle times, exit conditions
While CONDITION; Do statement ... done #进入循环: condition satisfied; #退出循环: Condition not satisfied
(4) Until loop statement
Until CONDITION; Do statement ... done #进入循环: Condition not satisfied #退出循环: condition satisfied
(5) Select loop
Select variable name [in menu Value list];d o ... done
(6) Dead loop; Read the contents of a file in a script
While:;d o ... donewhile read line;d o ... done </path/to/somefile
Loop Control statement:
Command |
Description |
Break n |
n indicates the number of layers to jump out of the loop, or omit N to jump out of the loop |
Continue N |
n means exiting to the nth level to continue the loop, if omitting n means skipping the loop, ignoring the remaining code of the loop, and entering the next loop of the loop |
Exit n |
Exits the current shell program, and N is the return value. n can also be omitted, in the next shell through $? Receive the value of this n |
return n |
Used in a function as a return value of a function to determine whether the function was executed correctly |
Multiple methods for generating random numbers
1, through the system environment variable ($RANDOM) implementation
[[email protected] ~]# echo $RANDOM 14099[[email protected] ~]# echo $RANDOM 18044
2. Generate random numbers through OpenSSL
[email protected] ~]# OpenSSL rand-base64 8gwz+fzb/qgw=[[email protected] ~]# OpenSSL rand-base64 8y5xvo8olmd4=
3. Get random numbers between (date)
[[Email protected] ~]# date +%s%n1498638876194441781[[email protected] ~]# date +%s%n1498638877282447419
4. Generate random numbers by/dev/urandom with Cksum
[Email protected] ~]# head/dev/urandom|cksum1747366268 803[[email protected] ~]# head/dev/urandom|cksum3313383146 2380
5. Generate random numbers from UUID
[Email protected] ~]# cat/proc/sys/kernel/random/uuid 3ca53d1d-463c-493f-8bb8-e5bf371a1ecd[[email protected] ~]# cat /proc/sys/kernel/random/uuid d67d83b6-8465-4ef5-bcb9-a8b24a521a88
6. Generate random numbers using the MKPASSWD included with expect
[Email protected] ~]# mkpasswd-l 9Fxfi; Nm82[[email protected] ~]# mkpasswd-l 9t7]utmaf2
Debug mode for Scripts
1) #/bin/bash-option script argument (first line of script)
-N Syntax Check mode
-v vebose mode, print all statements read by the shell
-X Trace mode, the command that the script actually executes after the replace operation is performed
2) Set-x Open Debugging function
Set +x turn off the Debug function (narrow the debug range)
3) shell built-in pseudo-signal:
Exit: Quit from a function or end of script
ERR: Execution failed
DEBUG: Before each command of the script executes
Capture signal: trap ' command ' signal
This article is from the "Wind and Drift" blog, please be sure to keep this source http://yinsuifeng.blog.51cto.com/10173491/1942764
Three, the Shell branch and the loop structure