Several shell programming little knowledge (shell common sense part)

Source: Internet
Author: User
Tags chmod
Reference: First, the user log 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

Three, the shell of the variable:
* 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 There are no line breaks when printing a line this is what we often use
/F Page Change
/R Enter
/T tables
/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 -



Today's introduction to the shell special characters reference
===============================
Special characters in the Shell have

1, $ dollar character
2,/back 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 a special meaning in double quotes. Double quotes don't work on the $ symbol
The single quotation mark can block the special meaning of the special word Fu, so that it can be displayed as the character itself, the reverse oblique
The bar can also be shielded from the special meaning of special characters, so that special characters lose special meaning.

Two,/back 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 be displayed as 1234
echo/' Show as '
Echo/"appears as double quotes
echo//Display 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 and the second is to use the/backslash to make it lose its effect.

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
Everyone noticed that I put all the other special characters together except for the first four special characters, because the first four special characters have special meanings in double quotes, so separate out the special characters if you want to output these special characters, 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.

So many people have noticed that all the special characters lose their special meaning in single quotes, if you want to output special characters but can't remember those special characters in double quotes can not output the original, it is recommended that you simply use single quotes.

Today's introduction to conditional test statements

One, if condition statement
Format:
An IF condition expression
Then #当条件为真时执行以下语句
Command List
else #为假时执行以下语句
Command List
Fi

The IF statement can also be nested using the

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 do multiple layers of nesting an if statement must be with a fi to indicate that the layer condition ends otherwise it can cause syntax errors
In conjunction with the preceding examples, the following are:
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 it means.
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 begins with a case to esac the middle can have more than one condition list function is the test string and and inside the pattern there is no match, have on the execution inside of the command list mode can also be * to express any string, each pattern inside the last 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 will compile the file a.c
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

Today's introduction to circular statements
One. While loop
While command format

While condition table
Todo
Command table
Done

Execution process

The shell first executes the condition table, and if the last statement of the condition table has an exit state of zero, the command in the Shield ring body is executed
Table, and then check the condition table, and if the exit status is zero, continue to execute, so that it repeats until the condition table
The exit state of the last statement is 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 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

In this loop, you can also use until as a test condition, which is exactly 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, which is also used in the following example.


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 list of strings separated by a space, and the shell executes a for loop each time from the name table
To assign a string to the loop variable as the value of the variable.
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 name
Word list.
Here's an example
For example, there are two directories in your computer, one is AA and the other is BB There are 5 identical files in both directories, but their
One of the directories in one or more of the files have just been modified, and now I forget just changed is that a few files, then I shoot a member Shing LUN onto Reef Guoco Envy to raise  oval  lazy? Procedures are 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
The break command does not perform the current loop body break the following statement exits from the current loop.
The continue command is that the program ignores the following statement in this body and executes it from the loop header.

One, command combination: parentheses and braces
There are two ways in the shell to combine commands together: parentheses and braces. Parentheses make the shell a child shell
To read and execute the enclosed name command. Opening and closing brackets regardless of where they appear on the command line, the shell will
Think they have a special combination of meaning. Parentheses or curly braces are only represented by enclosing them in double quotes
The original meaning. For example:

echo A (b)
There will be a grammatical error, and the output of a (b) string can only be enclosed
echo "A (b)"
or echo a "(" B ")"
This will be interpreted correctly by the shell.
What's the use of a combination command?
One, combining the command with parentheses
The combined commands of parentheses can create child processes to run the assembly, and it is useful to build the functionality of the subprocess because
The actions of a child shell in a composite command do not affect the values of the variables in the current shell.
For example:
The child process changes the working directory when it executes the combined command and executes a system command under the new working directory, executing
It can be done without having to return to the original working directory, because changes to the working directory of the child process do not affect the current working directory.

After the child process is created, the current environment is also passed to the child shell, which is exported to the environment by export in the current shell.
The variables are equally valid in the child shell.


Curly braces can also combine commands together. The left and right curly braces only appear as the first word of a command,
Shells, they contain special meanings.
Unlike parentheses, curly braces do not create a child shell, but are read by the current shell and executed by the enclosing
command. Sometimes the user wants to use the sequential output of a set of commands as input to another set of commands, which is very square with curly braces.
Then.
Whether you use parentheses instead of curly braces, the exit status is equal to the exit state of the last-enclosed command.


Second, commands that can be executed in the current shell

Users must be aware of commands that can be executed in the current shell when using the shell.
The commands that can be executed in the current shell are:

Break Case CD Continue
echo Eval exec exit
Export for if read
ReadOnly return set shift
Test times Trap Umask
Until wait while
: {}
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.