2.1 What is Shell
- Interpreted language, which is not suitable for jobs that are picky about execution time or have a large processor.
- Shell is a program that acts as an operation interface between users and Unix systems. File redirection only requires "<" and ", and pipelines can be represented by a" |, the output of the sub-process is "$ (...) ", all these details have been prepared for the user.
2.2 pipelines and redirection
2.2.1 redirect output data
- $ LS-L> lsoutput.txt: Save the lscommand output to a file named lsoutput.txt.
- $ PS> lsoutput.txt. This command will append the output of the ps command to the end of the file.
- File descriptor 0 represents a program standard input, file descriptor 1 represents a program standard output, and file descriptor 2 represents a program standard error output
2.2.2 redirect input data
- Not only can the output information be redirected, but also the input data can be redirected, such as $ more <killout.txt
2.2.3 MPs queue
- The pipeline operator "|" can be used to connect processes. UNIX is different from Dos. Processes connected by pipelines can run simultaneously, it will automatically coordinate with the flow of data streams between them
2.3 shell that can be considered as a programming language
2.3.1 interactive program
2.3.2 compile a script
- The comments in the program start with a "#" symbol and continue until the end of the line
- In shell program design, the exit command returns "0" to indicate that the operation is successful.
- UNIX usually seldom uses the file extension to determine the file type, which can be omitted completely ". sh ", or add a different extension to it. Shell doesn't care about it. The best way to know if they are script programs is to use the file command, such as" file first. sh"
2.3.3 set the script as an executable program
- Run the CHMOD command to modify the status (mode) of the prepared script file. This file can be executed by all users. $ chomd + X first. Sh
2.4 shell programming syntax
2.4.1 Variables
- In Shell, variables do not need to be declared before they are used. By default, all variables are considered and saved as strings, even if they are assigned numerical values. Shell and other workers convert "numeric" strings into correct values in sequence and operate them in the correct way.
- Because UNIX is a case-sensitive system, it remains the same in shell.
- In
In shell, you can add a "$" character before the variable name to get its content, and the echo command can output its content. You can add a "$" character before the variable.
Unless we are assigning values to the variable. Note: If the string contains spaces, you must enclose them in quotation marks. Note that there must be no spaces on both sides of the equal sign.
$ Salutation = Hello
$ Echo $ salutation
Hello
$ Salutation = "year dear"
$ Echo $ salutation
Year dear
$ Salutation = 7 + 5
$ Echo $ salutation
7 + 5
2.4.2 conditional test
2.4.3 Control Structure
2.4.4 Functions
2.4.5 command
2.4.6 Command Execution
2.4.7 instant documentation
2.4.8 debug the script program
2.5 SHELL Program Design Example
2.5.1 work requirements
2.5.2 Design
2.6 summary of this Chapter