1 Specifications for Shell scripts:
1 file suffix is. sh
2 first line is the command interpreter: #!/bin/bash
3 The first line below the comment write script basic information:
Script name, function, writing time, author, contact, etc.
2 Shell Execution Mode:
1 loading the script file with the command interpreter:
Bash file
2 absolute path or relative path
./demo.sh
Scripts need to have executable permissions and an interpreter selected
3 source or.
source./demo.sh
. demo.sh
Function: Load script file
3 Shell variables:
Variable definition:
Variable name = variable Value
Variable printing:
echo $ variable Name
Divided into: Local variables, global variables, built-in variables
3.1 Local variables:
Common variables:
1 Variable name = variable value variable must be contiguous without spaces and special characters
2 Variable name = ' variable value ' variable can contain spaces and special characters as -is
3 Variable name = "Variable Value" variable can reference other variable in discontinuous variable value
Command variables:
1 Variable name = ' Command '
2 variable name =$ (command)
3.2 Global Variables:
1 Viewing global variables:
Env
2 Defining global variables:
define the Post declaration first:
Demo=val
Export Demo
Direct Definition
Export Demo=val
3 Built-in variables:
$ A gets the name of the current script
$ #获取当前脚本的参数个数
$n Gets the nth parameter of the current script
$? Gets the execution of the last command 0 represents success
$$ Get the process PID when the script runs
[email protected] Get a list of parameters for the current script
$* Gets the string representation of all parameters of the current script
String interception:
new_str=$ (Str:start:length)
Truncate STR from start to intercept the length of
new_str=$ (Str:0-length)
Truncate the length of STR from the end
Default value:
var_name=$1# takes the first parameter of the current script
#如果没有var_name is assigned the default
Var_new=${var_name:-default}
Var_name=$1 #取当前脚本第一个参数
# no matter what Var_name is, force assignment to default
Var_new=${var_name:+default}
3.4 Ways to view variables:
1 $ variable name
privately used
2 "$ variable name"
use when calling variables
3 "${variable name}"
used in Scripts
4 variable operation:
1 Declaration read-only
readonly variable name
2 Deleting a variable
unset variable name
4 Shell validation actions:
mode 1:[expression] The expression must have spaces on either side
[1-eq 1]# 1 is equal to 1
echo $?# View previous sentence execution results
mode 2:test expression
Test 1-eq # See if 1 is equal to 1
echo $?# View previous sentence execution results
4.1 logic:
and && command 1 & ;& Command 2 Execute command 2
non | | Command 1 | | Command 2 If command 1 execution succeeds do not execute command 2
Command 1 Execute failed execute command 2
Span style= "font-size:16px" to 4.2 file expression:
[file expression filename]
E" Whether there is a
-F is a file
-D whether the directory
-W writable
4.3 Number comparison:
[N1-GT N2] N1 is greater than N2
-GT Greater than
-lt less than
-eq equals
-ne Not equal to
4.4 String
= = is equal
! = is inconsistent
-Z is 0
Whether the-n length is not 0
5 Process Control:
Operation:
Let N=n+1
Or
n=$ (($n + 1))
Single If branch:
If condition
Then execute the statement
Fi
Multi-If Branch
If condition
Then execute the statement
Elif conditions
Then execute the statement
Else EXECUTE statement
Fi
Case Multi-Branch
Case value in
Value 1)
EXECUTE statement
;;
Value 2)
EXECUTE statement
;;
Esac
Cycle:
Until conditions
Do execution statement
Done
6 functions
To define a function:
Func_name () {
function body
}
Call Function:
Func_name
There are parametric functions:
Func_name () {
args = $n
}
Call:
Func_name ARGS1 ARGS2
Shell Basic Syntax