Shell common knowledge

Source: Internet
Author: User
1. system environment variables after the user logs on to the system $ HOME user's directory $ PATH the directory searched for when executing the command $ TZ time zone $ MAILCHECK check every several seconds

1. system environment variables after users log on to the system

$ HOME user's directory $ PATH: The Directory searched during command execution $ TZ time zone $ How many seconds does MAILCHECK check for new letters? $ PS1 prompt number during command column $ PS2 when the command has not been completed, the Shell requires that the search path of the $ MANPATH man command be input again.

II. special variables

$0 execution name of this 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 this program $! Run the PID of the previous command $? The return value of the previous command.

3. variable in shell

* Any string? Any character [a-n] from a to n of any character [abc] a, B, and c

4. Special characters

B returned to c and printed a row without a line break. we often use the f form. r press enter t tabulation v vertical tabulation \ backslash itself.

V. determining file attributes

Format:-operator filename-e returns 1 if a file exists; otherwise, 0-r is returned; 1 if a file is readable; otherwise, 0-w is returned; 1 if a file is writable, otherwise, 0-x files are returned. if 0-o files are returned, 1 is returned. otherwise, 0-z files are returned. if 0-z files are returned, 1 is returned. otherwise, 0 is returned. -If the f file is a normal file, 1 is returned; otherwise, 1 is returned when the 0-d file is a directory file; otherwise, 0 is returned.

6. test the string

String 1 = String 2 true String 1 when two strings are equal! = String 2 when two strings are not equal to true-n string when the length of the string is greater than 0 true-z string when the length of the string is 0 true string when the string is not empty true

7. test the relationship between two integers

Number 1-eq number 2 two equal true Number 1-ne number 2 two unequal true Number 1-gt number 2 digit 1 greater than number 2 True Number 1-ge number 2 digit 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

 

Special characters in shell include:

1. $ USD 2, backslash 3, 'quotation marks 4, "double quotation marks 5, <,> ,*,?, [,]

The following is a one-to-one description.

1. $ symbol

1. echo $? The Last Command exit status is displayed. 2. echo "$? "The effect is the same as above 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 this parameter is not added, it is displayed as 1234 echo. 'echo "is displayed as double quotation mark 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 the system command to A variable A = 'Date' echo $ A. The display is not date, but the time string at that time. for example, there is A file. the content of A is as follows: ABCDEFG 1234456 abcdefg B = 'cat A | grep 100' # retrieve the line echo $ B that contains string 234 in file A will be displayed as 234 echo "$ B" will be displayed 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 non-special, one is to use single quotes to bring in, and the other is to use a 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 ''' or echo" echo "$" echo "\" echo "'" are 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 statements

Format: if condition expression then # execute the following statement command list when the condition is true else # if it is false, run the following statement command list fi if statement or 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

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 not a file or directory" fi the preceding example can also be changed to 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;... esac

The multi-condition statement starts from case and ends with esac. Multiple condition lists can be used to test whether the string matches the pattern in it, the command list mode in execution can also be "*" to represent any string, and the last point in each mode is the heart; double quotation marks are used to end; otherwise, a syntax error occurs.

The following is an example:

  1. Case $1 in
  2. *. C)
  3. Cc $1
  4. ;;
  5. *. Txt)
  6. Lpr $1
  7. ;;
  8. *)
  9. Echo "unknown type"
  10. 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

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:

  1. Sum = 0
  2. I = 0
  3. While true # true is the system keyword indicating true
  4. Do
  5. I = 'expr $ I + 1'
  6. Sum = 'expr $ Sum + $ I'
  7. If [$ I = "100"]
  8. Then
  9. Break;
  10. Fi
  11. Done
  12. Echo $ I $ Sum

The program shows 100 5050

The calculation of this program is to add 1 to 100

Next, let's change this program.

  1. Sum = 0
  2. I = 0
  3. While [$ I! = "100"]
  4. Do
  5. I = 'expr $ I + 1'
  6. Sum = 'expr $ Sum + $ I'
  7. Done
  8. Echo $ I $ Sum

The modified program operation result is the same as above, but the program is more concise than above.

In this loop, you can also use until as the test condition. it is exactly 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.

  1. Sum = 0
  2. I = 0
  3. Until [$ I = "100"]
  4. Do
  5. I = 'expr $ I + 1'
  6. Sum = 'expr $ Sum + $ I'
  7. Done
  8. 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, however, one or more files in one of the directories have just been modified. now I forget the files I just modified. then I can find them by known code. the procedure is as follows:

  1. For File in a1 a2 a3 a4 a5
  2. Do
  3. Diff aa/$ File bb/$ File
  4. 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

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.

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.