Shell common knowledge

Source: Internet
Author: User
Shell common knowledge-general Linux technology-Linux programming and kernel information. The following is a detailed description. Shell common knowledge
1. system environment variables after users log on to the system
$ HOME user's own directory
$ PATH: the directory to be searched during Command Execution
$ TZ Time Zone
$ MAILCHECK the number of seconds to check whether a new letter exists
$ PS1 prompt number in the Command Column
$ PS2
When the command has not been completed, Shell requires that the prompt number be entered again
$ MANPATH man command search path
Ii. Special Variables
$0 execution name of the program
$ N the nth parameter value of this program, n = 1 .. 9
$ * All parameters of this program
$ # Number of parameters of this program
$ PID of the program
$!
PID used to execute the previous command
$? The Return Value of the previous command.
3. Variable in shell
* Any string
? Any character
[Abc] One of the three: a, B, and c
[A-n] any character from a to n
4. Special characters
\ B Return
\ C is often used to print a row without line breaks.
\ F form feed
\ R press ENTER
\ T tabulation
\ V vertical tabulation
\ Backslash itself
V. Determining file attributes
Format:-operator filename
-If the file-e exists, 1 is returned; otherwise, 0 is returned.
-If the r file is readable, 1 is returned; otherwise, 0 is returned.
-W file write return 1, otherwise return 0
-1 is returned for executable-x Files; otherwise, 0 is returned.
-O: if the file belongs to the user, 1 is returned; otherwise, 0 is returned.
-If the length of the z file is 0, 1 is returned; otherwise, 0 is returned.
-F
If the file is a normal file, 1 is returned; otherwise, 0 is returned.
-If the d file is a directory file, 1 is returned; otherwise, 0 is returned.
6. Test the string
String 1 = string 2 true when two strings are equal
String 1! = String 2
True when two strings are not equal
-N string: true when the string length is greater than 0
-Z string: true when the string length is 0
String is true when the string is not empty
7. Test the relationship between two integers
Number 1-eq Number 2 equal to true
The number 1-ne number 2 is not true.
Digit 1-gt digit 2 digit 1 greater than digit 2 true
Number 1-ge number 2
Number 1 is true if it is greater than or equal to number 2
Number 1-lt number 2 Number 1 less than number 2 true
Number 1-le number 2 Number 1 less than or equal to number 2 true
8. Logic Testing
-A and
-O or
! Non
Special characters in shell include:
1. $ dollar sign
2. \ backslash
3. 'quotation marks
4. Double quotation marks
5 ,,*,?, [,]
The following is a one-to-one description.
1. $ symbol
1. echo $? Displays the exit status of the previous command.
2. echo "$? "Same effect
3. echo '$? 'Is $?
4. echo \ $? $?
5. echo "\ $? "Show $?
You may have seen that the $ symbol has special meaning in double quotation marks. Double quotation marks do not work for the $ symbol, but single quotation marks can block the special meaning of special characters so that they can be displayed as characters themselves, the backslash can also remove the special meanings of special characters, so that special characters do not have special meanings.
Ii. \ backslash
The backslash is used to block the special meaning of a special character so that it is the original character.
A = 1234
Echo \ $ A is displayed as $ A. If no value is added, 1234 is displayed.
Echo \ 'displayed'
Echo \ "is displayed as double quotation marks
Echo \ is displayed \
Iii. 'quotation marks
The function of anti-quotation marks is to replace commands and execute strings in the anti-quotation marks as commands. We often use shell programming to assign the execution result of system commands to a variable.
A = 'date'
Echo $ A shows the time string instead of the date.
For example, the content of file A is as follows:
ABCDEFG
1234456
Abcdefg
B = 'cat A | grep 234 '? #
Retrieve the row containing the string 234 in file
Echo $ B is displayed as 1234456
Echo "$ B" will show why?
Echo "\ $ B" will show why? Try it by yourself
IV ,"
Double quotation marks
There are some special characters in the system. To avoid referencing these special characters, these special characters are often caused by double quotation marks or single quotation marks, so that they do not have special meanings.
However, some special characters still have special meanings in quotation marks, which are caused by double quotation marks and do not work. The first four special characters listed in this article are in double quotation marks or special characters. In order to make it have no special meaning, one is to use single quotation marks, and the other is to use \ 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 expected result, because double quotation marks do not work for them, you can only output the prototype of these special characters
Echo '"'
Echo '$'
Echo '\'
Echo '''
Or
Echo "\""
Echo "\ $"
Echo "\\"
Echo "\'"
It is displayed as "$ \'
5. Other special characters
We have noticed that apart from the first four special characters, I put all the other special characters in one piece. This is because the first four special characters still have special meanings in double quotation marks, so I will explain them separately, if you want to output the prototype of these special characters, you can use double quotation marks or single quotation marks to make them lose their special meaning.
,*,?, [,] It has a special meaning for shell, but you can use double quotation marks to input these prototypes.
I. if
Condition Statement
Format:
If condition expression
Then # execute the following statement when the condition is true
Command list
Else # execute the following statement when false
Command list
Fi
If statements can also be nested
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 nest an if statement in multiple layers. It must end with a fi to indicate the condition of the layer. Otherwise, syntax errors may occur. The preceding example is as follows:
Here we first talk about the command test used in a condition statement to test whether the condition after test is true.
If test-f "$1"
Then
Lpr $1
Else
If test-d "$1"
Then
Cd $1
Lpr $1
Else
Echo "$1 is not a file or directory"
Fi
Fi
The above example can also be changed to the following:
If test-f "$1"
Then
Lpr $1
Elif test-d "$1" # elif is the same as else if
Then
(Cd
$1; lpr $1)
Else
Echo "$1 is not a file or directory"
Fi ??????
Do you understand the above examples?
If we save this example as prfile
Chmod + x prfile
Execute the program
./Prfile aaa
In this example, check whether your input parameter is a file. If it is a file, print it. If it is a directory, convert it to a directory and then print it. If it is not a file or a directory, a prompt is given.
Ii. Multi-condition test statement case
Format:
Case string in
Mode) command list ;;
Mode) command list ;;
....
Esac
The multi-Condition Statement is case
In the middle of starting to end with esac, multiple condition lists can be used to test whether the string matches the mode in it. If yes, the command list mode in the execution can also be the * sign to indicate any string, the last point in each mode is the heart; the end of double quotation marks; otherwise, a syntax error occurs.
The following is an example:
Case $1 in
*. C)
Cc $1
;;
*. Txt)
Lpr $1
;;
*)
Echo "unknown type"
Esac
If you save the preceding content in the abc file
Chmod + x abc
Run./abc a. c
The file a. c will be compiled.
Execute./abc readme.txt and the file will be passed through the printer
If I change the preceding content, do you know the execution result?
Case $1 in
*)
Cc $1
;;
*. Txt)
Lpr $1
;;
*. C)
Echo
"Unknown type"
Esac
1. while Loop
While Command Format
While condition table
Do
Command table
Done
Execution Process
Shell first executes the condition table. If the exit status of the last statement in the condition table is zero, execute the command table in the environment. After the execution, check the condition table, if the exit status is zero, the execution continues until the exit status of the last statement in the condition table is non-zero.
If the exit status is zero, the condition is True.
For example, if the content of the shell file is as follows:
Sum = 0
I = 0
While true # true is the system keyword indicating true
Do
I = 'expr $ I + 1'
Sum = 'expr $ Sum + $ I'
If [$ I = "100"]
Then
Break;
Fi
Done
Echo $ I $ Sum
Finally, this program displays
100 5050
The calculation of this program is to add 1 to 100
Next, let's change this program.
Sum = 0
I = 0
While [$ I! = "100"]
Do
I = 'expr $ I + 1'
Sum = 'expr $ Sum + $ I'
Done
Echo $ I $ Sum
The modified program operation result is the same as above, but the program is more concise than above.
In this loop, until can also be used as the test condition.
It is the opposite of the while test condition, that is, when the condition is false, the statement in the loop body will continue to be executed; otherwise, the loop body will exit. This example is also used below.
Sum = 0
I = 0
Until [$ I = "100"]
Do
I = 'expr $ I + 1'
Sum = 'expr $ Sum + $ I'
Done
Echo $ I $ Sum
When I is not equal to 100, the loop is when the condition is false. Otherwise, the loop exits. The first example is when I is not equal to 100.
Cycle, that is, when the test condition is true.
II. for Loop
Command Format:
For variable in Name List
Do
Command list
Done
The name list here is a list of strings separated by spaces. shell extracts a string from the name table each time it executes the for loop and assigns it to the loop variable as the variable value.
When writing a for statement, you can also omit the in name list section, which means that the current location parameter is used to replace the name list.
For example, there are two directories in your computer, one is aa, and the other is bb. There are five identical files in these two directories, but one or more files in one of the directories have just been modified. Now I forget the files I just modified.
So I can find it by the known sequence code.
The procedure is as follows:
For File in a1 a2 a3 a4 a5
Do
Diff aa/$ File bb/$ File
Done
The following is an example without a name list.
For
File
Do
Echo $ Filw
Done
The file content is saved in a. sh and can be executed
When we execute this shell program, the command line is as follows:
A. sh a1 a2 a3 a4 a5
The execution result is as follows:
A1
A2
A3
A4
A5
In this example, we can see that the command line parameters are read one by one.
Iii. Loop Control statements
Break
The command does not execute the statement under the break in the current loop to exit from the current loop.
Continue
The command is used by the program to ignore the following statements in the body and start execution from the loop header.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.