12.3 option analysis in shell script
The first method is to use the case statement for manual processing.
Method 2: Use the getopts command to analyze the options
Syntax format:
Getopts option-string variable
Option-string is a string that contains all single-character options. These characters are analyzed by getopts.
These options should be assigned a variable. The variable is the variable name assigned to the variable.
Getopts analyzes the options provided by the command line. The analysis process is as follows:
1) The getopts option checks all command line parameters and finds the characters starting.
2) When a parameter starting with "-" is found, the character following "-" is compared with the character given in Option-string.
Relatively.
3) if a match is found, the specified variable is set to an option; otherwise, variable is set "? "Character.
4) Repeat 1-3 until the search option is traversed.
5) After the analysis, getopts returns a non-zero value and releases it.
Another feature of getopts is that it can specify options that require additional parameters. Add ":" After the option in Option-string ":"
Character to achieve this function. In this case, after the option is analyzed, the additional parameter is set to the value of the variable optarg.
Use getopts
While F: O: V option:
Do
Case "$ option" in
F) F = $ option ;;
O) O = $ option ;;
V) V = $ option ;;
*) Echo "$ usage ";
Exit 1
;;
Esac
Done
12th hours input/output
When a command generates a write-to-terminal output, the program prints the output to the standard output (stdout ).
Error messages are not written to stdout. They are written to a special type output, which is called stderr ),
It is reserved for error information.
13.1.1 output to the terminal
1) echo
2) printf
13.1.2 output redirection
In shell scripts, commands are often captured and stored in files. When the output is stored in a file, you can easily
Edit and modify.
Redirection operator ">", "<"
">" Symbol redirection overwrites the data in the original file. You must pay attention to it when using it.
If you need to add data on the original basis, you can use ">" to append the data to the end of the file.
Redirection output to files and screens
In some cases, you must redirect the output of scripts to files and terminals at the same time. You can use the tee command.
Example: $ date | tee Now
Output the current date on the terminal and write the data to the file nowzhong.
13.2 Input
1) redirect the input to a file to read the input from the file.
2) read the input from the user.
3) redirects the output of one command to the input of another command.
13.2.1 input redirection
Command <File
13.2.2 read user input
One of the common tasks of shell scripts is to prompt the user to input and then read the user's response.
Format:
Read name
It reads a whole row of user input until the user enters the Enter key, which makes the row the value of the variable specified by name.
For example:
YN = Yes
Printf "do you want to play a gane [$ YN]? "
Read YN
Case $ YN in
[YY]) echo yes ;;
*) Echo "Maybe later .";;
Esac
13.2.3 MPs queue
In UNIX, most of the commands used to process files can read input from stdin, which allows users to use one program to filter input from another program.
Output.
You can use pipelines to redirect the output of one command to the input of another command. Several commands can be connected together with pipelines.
Example: $ who | grep sudo
Use the output of the WHO command as the input of the next command, and then filter the output to the terminal.
13.3 file descriptor
Standard input (stdin) 0
Standard output (stdout) 1
Standard Error (stderr) 2
Command 1> file1 2> file2
Stdout of the given command is redirected to file1, and stderr (error message) is redirected to file2
For file in $ files
Do
Ln-S $ file./docs>/tmp/ln. log 2>/dev/null
Here, the stdout of LN is added to the file/tmp/ln. log, and stdout is redirected to the/dev/null file.
Note: File/dev/null is a reshuffle file that can be used in all UNIX systems to delete the output.