A Linux shell script
Shell: command-line interpreter
(1) SH
(2) CSH
(3) Ksh
(4) Bash
Shell script: A collection of commands that accomplish a specific thing
Two run a shell script file
1. Add executable permissions, and then run the./shell script file
2. Use the shell to parse our script file, bash script file
Three shell scripting syntax
1. Variables
(1) User-defined variables
Variable name = Content
Characteristics:
<1> No data type
<2> cannot have spaces on either side of the assignment
<3> all assignments to shell variables are interpreted as strings
(2) Environment variables
Path is the default search path for the shell
Home record is the user home directory of the current user
(3) Position variable
The name of the $ A script
The first parameter passed to a script
The second argument passed to the script
...
$9
[email protected] Get all the parameters passed to the script
$* gets all parameters passed to the script
$# gets the number of arguments passed to the script
Attention:
When referencing the contents of a variable in the shell, you need to add "$", for example: $PATH
var1= $PATH
Var2= $HOME
var3= $VAR 1
var1= $VAR 2
Var2= $VAR 3
echo $VAR 1
Echo $VAR 2
2. Input and output
(1) input
Read Var1 var2 Var3
The first word entered by the user is assigned to VAR1, the second word is assigned to VAR2, and the remaining words are assigned to VAR3
(2) Output
echo "Hello word" output after wrap
Echo-n "Hello word" cancels the output after wrapping automatically
3. Calculation of data
+ , - , \* , / , %
Commands for operations: expr
4. The object of Judgment
(1) string
=,! =,-Z,-n
(2) Number
EQ, NE, GT, lt, GE, le
(3) File attributes
-D,-F,-R,-W,-X
5. Control statements
(1) Judgment statement
If [object of judgment]
Then
Command
Fi
-A and relationship
-O or relationship
If [$data-ge 256-a $data-lt 512]
Then
Echo $data
Fi
-----------------------------------------------------------------
Output redirection: Outputs the output to the specified file, rather than displaying it on the terminal
echo "string" > Log.txt first empty the Log.txt file and then write the string
echo "string" >> Log.txt writes the string in an appended way at the end of the Log.txt file
-----------------------------------------------------------------
If [object of judgment]
Then
Command
Else
Command
Fi
If [object of judgment]
Then
Command
Elif [object of judgment]
Then
Command
...
Elif [object of judgment]
Then
Command
Fi
Practice:
Enter a score to determine the grade of the grade
[80-100] A
[60-80) B
[0-60) C
If the result is not illegal [0-100], then prompts the user to enter the result is the illegal result
(2) Case statement
The contents of the case variable in
Matches 1)
Command
;;
Matches 2)
Command
;;
*)
Command
;;
Esac
Practice:
Enter a score to determine the grade of the grade
[80-100] A
[60-80) B
[0-60) C
If the result is not illegal [0-100], then prompts the user to enter the result is the illegal result
(3) Cycle
ASD F GHJ
For variable in Word table
Do
Command
Done
The number of cycles is determined by the number of words in the word list, each time the variable takes a word from the word list, and then executes the loop body until all the words are taken out.
For variable
Do
Command
Done
If the in Word table is omitted, the variable takes the word from the positional argument
(3) The number of files and subdirectories of the output copy
------------------------------------------------------------------------
While [expression]
Do
Command
Done
The command is executed only when the expression is true, and if it is false, the loop ends
------------------------------------------------------------------------
Break 2
Continue 2
Two source commands
1.bash test.sh or./test.sh It is a child process (shell process) that is created to interpret test.sh this script
The current shell (bash on the terminal) does not affect the end of the script after it is run
2.source test.sh or. Test.sh It is the shell of the current terminal to interpret the script, which will affect the current shell after the script is run.
Three shell functions
1. Function definition
Function name ()
{
Command
...
return numeric value
}
2. Function calls
Value= ' function name parameter 1 parameter 2 ... '
Value values The string inside the function that is output via echo
Function Name parameter 1 parameter 2 ...
Through $? Get the return value of a function
Question: How do I get the arguments passed inside the shell function?
Answer: By location variable ($ ...) To get the arguments passed
Note: Variables in the shell are global by default, and if you only want variables to be valid inside the function, you need to use local to modify the variables
While True
Do
Done
Shell Basic Command Summary