Small knowledge of shell programming

Source: Internet
Author: User
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

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 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 0, otherwise return 1
-R file can be read to return 0, otherwise return 1
-W file can write back 0, otherwise return 1
-X file can return 0, otherwise return 1
-O file belongs to the user himself return 0, otherwise return 1
-Z file length is 0 return 0, otherwise return 1
-F file returns 0 for normal file, otherwise returns 1
-D file returns 0 when the catalog file is returned, otherwise 1

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 -

Reference to Shell special characters
===============================
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 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.

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 "\" "

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.