Shell Common knowledge
First, the user login into the system after the system environment variables
$HOME user's own directory
$PATH directory to search for when executing commands
$TZ Time Zone
$MAILCHECK every few seconds to check for new letters
$PS 1 The prompt number at the command column
$PS 2 When the command is not finished, the Shell asks for the prompt number to be entered again
Search path for $MANPATH man command
Second, special variables
$ The execution name of this program
$n the nth parameter value of this program, n=1..9
$* all parameters of this program
$# the number of parameters for this program
$$ the PID of this program
$! PID that executes the previous instruction
$? Executes the return value of the previous instruction
Iii. the variable in the shell
* Any string
? One arbitrary character
[ABC] A, B, c one of the three
[A-n] Any character from A to n
Four, several special characters represent
\b Return
\c without line breaks when printing a line we often use
\f Page Change
\ r Carriage Return
\ t tab
\v Vertical Tabulation
\ backslash itself
V. Determining the attributes of a document
Format:-operator filename
-e file exists to return 1, otherwise return 0
-R file can be read to return 1, otherwise return 0
-W file can write back 1, otherwise return 0
-X file can return 1, otherwise return 0
-O file belongs to the user himself return 1, otherwise return 0
-Z file length is 0 return 1, otherwise return 0.
-F File returns 1 for normal file, otherwise returns 0
-D file returns 1 when the catalog file is returned, otherwise 0
Vi. Test String
String 1 = string 2 True when two strings are equal
String 1!= String 2 True when two strings are unequal
-N string is true when the length of the string is greater than 0
-Z String True when the length of the string is 0
String is true when string strings are Non-null
Seven, test two integer relationships
Number 1-eq number 22 equals True
Digital 1-ne Number 22 number is true
Digital 1-GT number 2 number 1 greater than number 2 is true
Digital 1-ge number 2 number 1 greater than or equal to number 2 is true
Digital 1-LT number 2 number 1 less than number 2 is true
Digital 1-le number 2 number 1 less than or equal to number 2 is true
Viii. Logical Testing
-A and
-O or
! Non -
Special characters in the Shell have
1, $ dollar character
2, reverse Slash
3, ' inverted quotation mark
4, "double quotes
5,, *,?, [,]
Let me make a list of the following
One, $ symbol
1, echo $? The previous instruction exit status is displayed
2, echo "$" effect Ibid.
3, Echo ' $? ' shows $?
4, Echo \$? Show $?
5, echo "\$?" shows the $?
You may have seen the $ symbol has special meaning in double quotes double quotes do not work with the $ symbol and single quotes can mask the special meaning of the special character Fu. So that it can be displayed as the character itself, the backslash can also mask the special meaning of special characters, so that special characters lose special meaning.
Second, reverse slash
The effect of a backslash is to mask the special meaning of a special symbol character so that it is still the original character.
a=1234
echo \ $A shown as $a if not added \ will display as 1234
echo \ ' shows as '
echo \ "appears as double quotes
echo \ \ is displayed as \
Three, ' inverted quotes
The function of the inverted quotation mark is to replace the command, to execute the string in the inverted quotation mark as a command, and we often use the Shell programming to assign the result of the system command to a variable
A= ' Date '
echo $A not showing date, but time series.
For example, the contents of a document A are as follows
ABCDEFG
1234456
Abcdefg
B= ' cat A|grep 234 ' #
Retrieves the row containing the string 234 in file a
Echo $B will appear as 1234456
echo "$B" will show why?
echo "\ $B" will show why? Readers try it on their own
Four, "double quotes
There are special characters in the system that are often used in double quotes or single quotes to avoid referring to these special characters so that they do not have special meaning.
However, there are some special characters that have special meanings in quotation marks, and it does not work with double quotes. The first four special characters listed in this article are in double quotes or special characters. In order to make it do not have a special meaning one is the introduction of single quotes to the second is to use the backslash to make it useless.
For example, we want to output these special characters as they are
echo "" "
echo "$"
echo "\"
echo "'"
The above is not the result you expect, because double quotes don't work for them, you can only do this to output the original form of these special characters.
Echo ' "'
Echo ' $ '
echo ' \ '
echo ""
Or
echo "\" "
echo "\$"
echo "\"
echo "\"
will appear as "$ \" respectively.
Five, other special characters
It was noted that in addition to the first four special characters, I put the other special characters in a piece, because the first four special characters in double quotes or have special meaning, so separate out the special characters, if you want to output these special characters of the prototype, You can use double quotes or single quotes to make them lose their special meaning.
, *,?, [,] has a special meaning for the shell but you can use double quotes to enter these prototypes.
One, if
Conditional statement
Format:
An IF condition expression
Then #当条件为真时执行以下语句
Command List
else #为假时执行以下语句
Command List
Fi
The IF statement can also be nested using the
If
Conditional expression 1
Then
If condition expression 2
Then
Command List
Else
If condition expression 3
Then
Command List
Else
Command List
Fi
Fi
Else
Command List
Fi
You can do multiple layers of nesting an if statement must be with a fi to indicate that the end of the layer condition would otherwise cause grammatical errors combined with the preceding examples as follows:
Let's start with a command test that is used in a conditional statement to indicate whether the condition after test tests is true
If Test-f "$"
Then
LPR $
Else
If test-d "$"
Then
CD $
LPR $
Else
echo "is not a file or directory"
Fi
Fi
The above examples can also be changed as follows
If Test-f "$"
Then
LPR $
Elif test-d "$" #elif with else if
Then
(CD
$1;LPR $)
Else
echo "is not a file or directory"
Fi
The above examples do not know if you understand what is the meaning of it?
If we now save this example as Prfile
chmod +x Prfile
Execute the procedure just now
./prfile AAA
This example is to check whether your input parameter is a file if it is on the print if it is a directory first to the directory and then print if that is not a file or a directory to give a hint
second, multiple conditions test statement case
Format:
Case string in
mode) command list;;
mode) command list;;
....
Esac
A multiple conditional statement is a case
Start with ESAC end the middle can have more than one condition list function is to test the string and and inside the pattern there is no match, have on the execution inside the command list mode can also be * to express any string, each pattern inside the final to heart;; Double quotes end, or a syntax error occurs.
Examples are as follows:
Case is in
*.C)
CC $
*.txt)
LPR $
*)
echo "Unknown type"
Esac
If the above content is saved in the document ABC
chmod +x ABC
Execute./ABC A.C
The file A.C will be compiled
Execute./ABC Readme.txt will pass the file through the printer
If I were to change the above, would you know the result of its execution?
Case is in
*)
CC $
*.txt)
LPR $
*.C)
Echo
"Unknown type"
Esac
one. While loop
While command format
While condition table
Todo
Command table
Done
Execution process
The shell first executes the condition table, if the last statement in the condition table has an exit state of zero, the command table in the Shield ring is executed, and then the condition table is checked, and if the exit status is zero, it repeats until the last statement in the condition table has an exit state of not 0.
The exit status is zero and the condition is true.
For example, if the contents of the shell file are as follows:
Sum=0
I=0
While True #true是系统的关键词 represents true
Todo
i= ' expr $i + 1 '
sum= ' expr $Sum + $i '
if [$i = "100"]
Then
Break
Fi
Done
echo $i $Sum
Finally, this program shows the
100 5050
The operation of this program is to add 1 to 100.
Here's how to change this program again.
Sum=0
I=0
While [$i!= "100"]
Todo
i= ' expr $i + 1 '
sum= ' expr $Sum + $i '
Done
echo $i $Sum
The result of the modified program is the same as the above but the program is more concise than the above
You can also use until as a test condition in this cycle.
It happens to be the opposite of the while test condition, that is, the statement that executes the loop body when the condition is false, or exits the loop body, and this example follows.
Sum=0
I=0
until [$i = "100"]
Todo
i= ' expr $i + 1 '
sum= ' expr $Sum + $i '
Done
echo $i $Sum
When I do not equal 100, the loop is when the condition is false, or it exits, and the first example is when I is not equal to 100
Time loop, which is when the test condition is true.
two. For Loop
Command format:
For variable in name list
Todo
Command List
Done
The list of names here is a space-delimited list of strings that the shell assigns to the loop variable as the value of the variable each time it executes a for loop by taking a string out of the name table in turn.
When writing a for statement, you can also omit the In Name list section, which means that the current position parameter is substituted for the list of names.
For example, there are two directories in your computer, one is AA, the other is BB There are 5 identical files in both directories, but one or more files in one directory have just been modified, and now I forget to change those files.
, then I am looking for a known sequence code.
The procedure is as follows:
For File in A1 A2 A3 A4 A5
Todo
Diff aa/$File bb/$File
Done
Here's another example with no list of names.
For
File
Todo
Echo $FILW
Done
The contents of the file are saved in a.sh and executable
We execute this shell program with the following command line:
A.SH A1 A2 A3 A4 A5
The results of the implementation are as follows:
A1
A2
A3
A4
A5
You can see from this example that the parameters of the command line are read one at a time
three. loop control Statement
Break
command does not perform the current loop body break the following statement exits from the current loop.
Continue
A command is a program that ignores the following statements in this body and executes it from the loop header.