In the open-source Unix system usually for the convenience of management will be a lot of use of shell script, and in peacetime writing script when the fast and effective is a lot of built-in variables can be used, here is a simple summary of the arrangement, so that in peacetime writing script, quickly consult, The built-in variables for the shell in POSIX are as follows:
variables |
meaning |
#
|
Number of parameters for the current process |
@
|
Command-line arguments passed to the current process |
-
|
Hyphen, giving shell parameter options when referencing
|
?
|
Exit status of the previous command |
$
|
The process number of the shell process, typically using the-bash process |
0
|
Name of the Shel program
|
!
|
Process number of the most recent background command
|
Env
|
Once referenced, it is used in the interactive shell
|
HOME
|
root directory, root user is/root
|
Ifs
|
Field divider, equivalent to (tab) key
|
Lang
|
Default name of the current locale |
Lc_all
|
The name of the current locale |
Lc_collate
|
The current locale name used to sort the characters |
Lc_messages
|
The name of the current language for the output information
|
Lineno
|
The line number of the row that was just executed in the script or function
|
Nlspath
|
The directory location for the current language of the output information |
PATH
|
Command Lookup Path
|
PPID
|
Process number of the parent process
|
PS1
|
The main command prompt string, default is "$", root privilege is "#"
|
PS2
|
Line continuation prompt string ">"
|
PS4
|
The string of traces executed with Set-x, which is "+" by default, is typically used when testing the trace script with Sh-x *.sh
|
Pwd
|
Current working directory
|
If you don't understand what the above variable is, Echo looks
[[email protected] data]# echo $PWD/data[[email protected] data]# ps-ef|grep ' echo $$ ' root 16174 16172 0 00:37 PTS/1 00:00:00-bashroot 22598 16174 0 13:37 pts/1 00:00:00 ps-efroot 22599 16174 0 13:37 pts/1 00:00:00 gr EP 16174
There are 6 kinds of end states in POSIX:
Value
|
Significance
|
0
|
Command successfully exited
|
>0
|
Failure during a redirect or a variable, command, etc unwind
|
1-125
|
command does not exit successfully
|
126
|
Command found but the file could not be executed
|
127
|
Command not Found
|
>128
|
The order was killed by the receipt of the signal
|
Here's a simple example:
[[email protected] test]# lstest1[[email protected] test]# ls-l test1-rw-r--r--1 root root 0 09-16 13:51 test1[[email p Rotected] test]# echo $?0[[email protected] test]# ls-l test2ls:test2: No file or directory [[email protected] test]# echo $? 2
And the test expression is more important, usually write script when a large number of if judgment will be used to
Operator
|
If ... is True
|
String
|
string is not NULL
|
-B File
|
File is a block of device files
|
-C file
|
File is a character device files
|
-D File
|
File is a directory
|
-E File
|
File exists
|
-F File
|
File is generic
|
-G file
|
File has set Setgid bit
|
-H file
|
File is a symbolic connection
|
-L File
|
Ditto
|
-N String
|
string is non-null |
-P File
|
File is FIFO
|
-R File
|
File is readable
|
-S file
|
File is the socket
|
-S file
|
File is not empty
|
-T N
|
File descriptor n points to a terminal
|
-U file
|
File has set setuid bit |
-W File
|
File is writable
|
-X File
|
File is executable
|
-Z String
|
string is null
|
S1 = s2
|
String S1 is the same as S2
|
S1! = S2
|
String S1 is not the same as S2 |
N1-eq N2
|
Integer n1 equals integer n2
|
N1-ne N2
|
Integer n1 not equal to integer n2 |
N1-lt N2
|
N1 less than N2
|
N1-GT N2
|
N1 greater than N2
|
N1-le N2
|
N1 less than or equal to N2
|
N1-ge N2
|
N1 greater than or equal to N2
|
This article from "Technical essay" blog, declined reprint!
Common variables, end states, and test expressions under the shell in POSIX