Common shell programming tips-general Linux technology-Linux programming and kernel information. For more information, see the following. 1. system environment variables after the user logs 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 indicates the prompt number when Shell requests to re-input when the command has not been completed
$ 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.
-If the f 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
Number 1-ge number 2 Number 1 greater than or equal to number 2 true
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
Introduction to shell special characters
====================================
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 $?
As you may have seen, 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 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 special meanings for shell, but you can use double quotation marks to input these prototypes.
Have you noticed that all special characters have no special meaning in single quotes? If you want to output the original form of special characters but cannot remember that these special characters cannot output the original form in double quotes, we recommend that you use single quotes.
Introduction to conditional test statements today
I. if condition statements
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 condition 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 perform multi-layer nesting. An if statement must be associated with a fi, indicating that the condition at the layer ends. Otherwise, a syntax error may occur. The preceding example is as follows:
Here we first talk about the command test used in a Condition Statement, which indicates 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
This example is to check whether your input parameter is a file. If it is a file, print it. If it is a directory, convert it to the directory before printing. 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 ends with esac starting with case. Multiple condition lists are available in the middle. The function is to test whether the string matches the mode in it. If yes, execute the command list in it, the pattern can also be a "*" sign, indicating any string. The last character in each pattern must be the heart. Double quotation marks are used to end. 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
Execute./abc a. c to compile the file a. c.
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
Today we will introduce loop statements
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
In the end, this program shows 100 5050. The operation 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 Operation Result of the modified program 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, which is the opposite of the while test condition, that is, when the condition is false, the statement in the loop will continue to be executed, otherwise, exit the loop body. 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 a loop when the condition is false. Otherwise, the loop is exited. The first example is a loop when I is not equal to 100, that is, a loop 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.
The following is an example.
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 am relying on the reseller to raise a fund for Ke yunnai, the reef in the xianxian region? Elliptical? Lazy? 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 the command line is as follows when we execute this shell program:
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
The break command does not execute the statements in the current cycle following the break.
The continue command is used by the program to ignore the following statements in the current cycle and start execution from the loop header.
I. Command combination:
There are two methods in parentheses and curly shells to combine commands: parentheses and curly brackets. parentheses enable shell to create a sub-shell to read and execute the name command. no matter where the left and right brackets appear in the command line, shell considers them as special combinations. it indicates the original meaning of parentheses or curly brackets only when they are enclosed and referenced in double quotes. for example:
Echo a (B)
Syntax errors may occur. to output a (B) strings, you must enclose them.
Echo "a (B )"
Or echo a "(" B ")"
In this way, it can be correctly explained by shell.
What is the function of using combined commands?
2. use parentheses to combine commands
The combination command of parentheses can create sub-processes to run the combined program. It is very useful to create sub-processes, because the sub-shell operations in the Combined Command do not affect the values of various variables of the current shell.
For example:
The sub-process changes the working directory when executing the combined command, and runs a series of commands in the new working directory. After the command is executed, it does not have to return to the original working directory, because the change of the sub-process working directory does not affect the current working directory.
After a sub-process is created, the current environment is also passed to the sub-shell. The variables output to the environment using export in the current shell are equally valid in the sub-shell.
The curly braces can also be used to combine commands. The left and right curly braces contain special meanings only when they appear as the first word of a command.
Unlike parentheses, curly braces do not create subshells, but are read and executed by the current shell. sometimes you want to use the sequential output of a group of commands as the input of another group of commands. It is very convenient to use curly brackets. whether the parentheses are used or not, the exit status is equal to the exit status of the last enclosed command.
3. commands that can be executed in the Current shell
When using shell, you must understand the commands that can be executed in the current shell. The commands that cannot be executed in the Current shell include:
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.