1 Opening
The program must start with the following line (must be in the first line of the file):
#!/bin/sh
The symbol #! is used to tell the system that the parameter behind it is the program used to execute the file. In this example we use/BIN/SH to execute the program.
When writing a script, you must also make it executable if you want to execute the script.
To make the scripting executable:
Compile chmod +x filename so you can use./filename to run
2 Notes
In shell programming, a sentence that begins with # represents a comment until the end of the line. We sincerely recommend that you use annotations in your programs.
3 variables
In other programming languages, you must use variables. In shell programming, all variables are made up of strings, and you do not need to declare the variables.
#对变量赋值:
A= "Hello World"
# now print the contents of variable a:
echo "A is:"
Echo $a
$# is the number of arguments passed to the script. $ is the name of the script itself. $ is the first parameter passed to the shell script, and the second argument that is passed to the shell script is [email protected] is a list of all the parameters passed to the script $* is to display all parameters passed to the script in a single string, unlike positional variables, which can be more than 9 $$ is the current process ID number for the script to run? is to display the exit status of the last command, 0 means no error, others indicate an error
4 Environment variables
Variables processed by the EXPORT keyword are called environment variables.
5 shell command and Process Control
There are three types of commands that can be used in shell scripts:
1) Unix command: Any UNIX command can be used in a shell script
2) Concepts: piping, redirection and Backtick
Pipe (|) The output of one command as input to another command.
grep "Hello" file.txt | Wc–l
Searches for a row containing "Hello" in File.txt and calculates its number of rows.
Here the output of the grep command as input to the WC command
Redirect: Outputs the result of the command to a file instead of the standard output (screen).
> Write files and overwrite old files
>> add to the end of the file, preserving the contents of the old file.
3) Process Control
Read SH script