One, Shell script
Script: Can execute the file, after running can implement some function (command stack, non-interactive)
Standardize the general composition of shell scripts
#! Environmental Statement (Sha-bang)
# comment Text
Executable code
Second, simply write a shell script, according to international practice, or output Hello World
[Email protected] ~]# vim/root/1.sh
#!/bin/bash
echo Hello World
[Email protected] ~]# chmod +x/root/1.sh
[Email protected] ~]#/root/1.sh
Pipeline Delivery
Use | Pipeline operation
– The standard output from the previous command is given to the latter command to process
Third, redirect output
Collect only the correct output from the previous command
2>: Collect only the error output from the previous command
&>: Collect errors and correct output from the previous command
/dev/null #Linux黑洞设备, designed to collect not output results
Variables: In order to increase the ability of the script's applicable environment, increase the flexibility of the script, convenient.
Variables: containers, with immutable names, store changed values
Variable name = changed value
Use variable: $ variable name
To reduce the difficulty of using scripts, you can create interactive
READ: Can generate interaction to assign the contents of the keyboard input to a variable
What is a variable
Values that are stored as immutable names that may vary
– Variable name = variable Value
– Easy to reuse a value with a fixed name
– Improved adaptability to mission requirements and operational environment changes
Considerations when setting a variable
– If the specified variable name already exists, it is equivalent to re-assigning a value to this variable
– Don't have spaces on both sides of the equals sign
– Variable names are made up of letters/numbers/underscores, case-sensitive
– Variable names cannot start with a number, do not use keywords and special characters
Basic format
– Reference variable Value: $ variable Name
– View variable values: Echo $ variable name, echo ${variable name}
Types of variables
Positional variables
Command-line arguments (non-interactive pass-through values) that are provided when the script is executed
Pre-defined variables
The execution information used to save the script
– Use these variables directly
– You cannot assign values directly to these variables
$# The number of position variables that have been loaded
$* values for all positional variables
$? Status value after program exit, 0 indicates normal, other value is abnormal
$ (): The output of the command, as a parameter
Common Test Options
Check file status
-e: Document exists as true
-D: Document exists and is directory-True
-F: Document exists and is file-true
-r: Document exists with Read permission true
-W: Document exists with Write permission true
-X: Document exists with Execute permission true
Compare integer size (with E equals two words, g means greater than, L is less than)
-GT: Greater Than
-ge: greater than or equal to
-eq: Equals
-ne: Not equal to
-LT: Less than
-le: Less than or equal to
string alignment
= =: Equal
! =: Not Equal
For loop structure
Loop structure: The repeated execution of the statement, loop to execute
For variable name in value list
Do
Command sequence
Done
Get started with simple shell scripting