Function libraries, signals and traps, file processing, arrays, and security for advanced shell script Programming
1. The function library puts all the functions that need to be used into a file, and each script starts with this file. For example:
#!/bin/shscope(){local soc=1gblsoc=2echo "local soc in func = $soc"echo "global gblsoc in func = $gblsoc"}another_scope_function(){echo "This is another_scope_func..."}yet_another_function(){echo "yet_another_function..........."}
You can access all these functions as follows:
#! /Bin/bash ../lib/scope // point (.): contains the function library file scopeanother_scope_functionyet_another_functionexit 0
<1>Getopts
Getopts is a built-in shell tool used to check whether the options passed to the command line are valid. The syntax is as follows:
Getopts opstring name
Getopts uses two variables to track everything:
Optind stores the next parameter index to be processed
If optarg requires a parameter, getopts places it in this variable.
#!/bin/shwhile getopts "xy:z:" namedo echo "$name" $OPTIND $OPTARGdone
$./P213-XY "one"-z "two"
Getopts (1). Check all command line parameters and find the parameters starting with the character.
(2) When a parameter starting with "-" is found, compare the string after "-" with option-string.
(3) If a matching match is found, the specified variable is set. Otherwise, the variable is set? Character.
(4) Repeat 1 to 3 until all options are processed.
2. Signal and trap
When the shell program runs, it may receive various signals, some from the operating system, some from the keyboard, and the shell immediately stops running after receiving the signal. However, in some cases, you may not want the program to stop running and quit immediately when the signal arrives. Instead, he can ignore this signal and keep running, or clear some operations before exiting. The trap command allows you to control the behavior of your program after receiving the signal.
The format is as follows:
Trap 'COMMAND; command 'signal-Number
Trap 'COMMAND; command 'signal-name
Trap signal-Number
Trap signal-name
The following two forms are mainly used for signal reset, that is, to restore the default behavior for processing the signal. Note that if the command after trap is enclosed in single quotes, the command is executed only when the specified signal is captured. If it is double quotation marks, the variable and command can be executed to replace it during the trap setting.
The following table compares the signal numbers and names provided by the system:
1) sighup 2) SIGINT 3) sigquit 4) sigill 5) sigtrap 6) SIGABRT 7) sigbus 8) sigfpe
9) sigkill 10) SIGUSR1 11) sigegv 12) sigusr2 13) sigpipe 14) sigalrm 15) sigterm 17) sigchld
18) sigcont 19) sigstop ......
3. File Processing
<1> file Detection
[Option file]
Expression
-D file: if the file exists and is a directory
-E FileIf file exists
-R FileIf the file exists and is readable
-S fileIf the file exists and the file size is greater than 0
-W file: if the file exists and can be written
-X file: if the file exists and can be executed
<2> clear files and retrieve various information
Use the trap command to clear temporary files
#!/bin/shtmpFile=/tmp/sigtrap$$cat > $tmpFilefunction removeTemp(){ if [-f "$tmpFile"] then echo "Strong out the temp file" rm -f "$tmpFile" fi }trap removeTemp 1 2exit 0
4. Array
<1> three methods for declaring Arrays
Array1 [Index] = Value
Array2 = (value1 value2 value3 value4)
Array3 = ([0] = value1 [13] = value2 [7] = value3)
<2> unreference an array
Obtain the data at a specific index position in the array, as follows:
$ {Array [Index]}
Value =$ {array3 [13]}
Echo "$ value"
Use specific symbols to determine all data in an array
Arrayelements =$ {array2 [@]}
Arraylength =$ {# array2 [@]}
You may not want to return all elements, but only need data in one of the returned results.
$ {Array [@]: 3}
// The first row returns data from all the remaining elements starting from the fourth element.
$ {Array [@]: 3: 2}
// Returns the values of the two elements after the fourth element.
$ {! Array [@]} // obtain the index value contained in the array
Security: Restricted Shell
#! /Bin/sh
CD/usr/include
Echo "'pwd '"
Set-R
CD
Echo "'pwd '"
Set + R
Exit 0
Supplement:
1. Special parameters $ * and $ @: These two allow access to all command line parameters at a time. Unless they are enclosed in double quotation marks (""), they are summarized as follows:
$ *: All command line parameters are specified.
$ @: All command line parameters are also specified.
"$ *": Obtain the entire parameter list as a parameter.
"$ @": Gets the entire parameter list and separates it into different parameters.
2. Double quotation marks ", single quotation marks", and reverse quotation marks"
Single quote ': All characters enclosed by single quotes are used as common characters. After special characters are enclosed by single quotes, the original meaning will also be lost.
Description of common characters.
Double quotation marks: A character enclosed by double quotation marks. Except for "$", and ", these characters are still special characters and retain their special functions.
Common characters.
Backquote ': Used for the same effect as $, for example, $ PWD and 'pwd.