Location and special variables for bash programming under Linux (iv)
1.bash Position Variable:
$, $, ... The 1th parameter, the second argument, ...
Shift poll substitution, culling
Example: Using a script to interpret the meaning of positional variables, create the following script:
Nano shift.sh Create a script file and add the following:
#!/bin/bash
#
If [$#-lt 5]; Then the script is followed by no less than 5 parameters
echo "./shift.sh ARG1 .... ARG5 "
Exit 5
Fi
echo "args:$1 $ $4 $" shows 1th to 5th parameters respectively
ECHO Displays the first parameter after the script
#shift Note, default forward culling of 1 parameters
Shift 2 rejects 2 parameters, 3rd parameter changes to 1th parameter
echo $ shows the 1th argument behind the script, which is actually the 3rd parameter of the script
Shift 2 rejects 2 parameters, 5th parameter changes to 1th parameter
echo $ shows the 1th argument behind the script, which is actually the 5th parameter of the script
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/7E/FC/wKioL1cPMjXxwb5IAADrAi4u5dU967.jpg "style=" float: none; "title=" shift1.jpg "alt=" Wkiol1cpmjxxwb5iaadrai4u5du967.jpg "/>
./shift 1 2 3 4 5 execution results are shown below
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7E/FC/wKioL1cPMjWgDlRNAACW0NRKP1E980.jpg "style=" float: none; "title=" shift2.jpg "alt=" Wkiol1cpmjwgdlrnaacw0nrkp1e980.jpg "/>
2.bash Special variables:
$?: Execution status Code return value
$#: Number of parameters
$*: Parameter list
[email protected]: parameter list
Example: Create a script, followed by at least one parameter, and display the number of parameters and list, or exit the script
Vim special.sh Creating a script file
#!/bin/bash
#
If [$#-lt 1]; Then
echo "usage:./special.sh ARG1 [ARG2 ...]"
Exit 9
Fi
echo "\$# is $#" displays the number of parameters
echo "\$* is $*" list
echo "\[email protected] is [email protected]" list
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/FC/wKioL1cPOXDDRwvqAADN25GlhlE144.jpg "title=" Specia1. JPG "alt=" wkiol1cpoxddrwvqaadn25glhle144.jpg "/>
chmod a+x special.sh granted execution rights
./special/etc/passwd/etc/shadow/proc
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/FF/wKiom1cPONaRK7P2AAEeGNEIr_4250.jpg "title=" Special11.jpg "alt=" Wkiom1cponark7p2aaeegneir_4250.jpg "/>
3. File test:
-e file: Test files exist
-F File: Test files are normal files
-D FILE: Tests whether the specified path is a directory
-R File: Tests whether the current user has read access to the specified file;
-W File: Tests whether the current user has write access to the specified file;
-X file: Tests whether the current user has execute permission on the specified file;
4. Script Exit command
Exit n N uses 0-255 exit status code value
PS: If the script does not explicitly define the exit status code, then the exit code of the last command executed is the exit status code of the script;
Example: Specify a file: If it is a normal file, it will display files, if it is a directory, display directory;
Otherwise, this is an unrecognized file; Please create such a script file
Vim exist.sh Creating a script file
#!/bin/bash
File=/etc/rc.d/rc.sysinit
if [!-e $FILE]; Then determine if the file exists
echo "NO such $FILE"
Exit 88 does not exist exit current script, exit code is 88, can write freely between 0-255
Fi
If [-f $FILE]; Then determine whether the file
echo "$FILE is FILE"
elif [-D $FILE]; Then determine if the directory
echo "$FILE is directory"
Else
echo "I don't known this $FILE" file not known
Fi
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/FF/wKiom1cPNHaDP_eRAAEvXb4lMVI070.jpg "title=" Exist1.jpg "alt=" Wkiom1cpnhadp_eraaevxb4lmvi070.jpg "/>
5. Test Scripts
Bash-n Script syntax error checking:
Bash-x script: Stepping through steps
Bash-x exist.sh test Script execution steps
This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1763764
Location and special variables for bash programming under Linux (iv)