Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the common knowledge of Shell?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what is the common knowledge of Shell, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Common knowledge of Shell

First, the system environment variables after the user logs in to the system

$HOME user's own directory

Directory searched by $PATH when executing the command

$TZ time zone

How often does MAILCHECK check if there are any new letters

The prompt number of $PS1 on the command line

PS2 the prompt number when Shell asks for re-input when the command is not finished.

The search path for the $MANPATH man instruction

2. Special variables

$0 the execution name of this program

The nth parameter value of this program, nimble 1.. 9

$* all parameters of this program

$# the number of parameters of this program

$PID of this program

$! The PID that executed the previous instruction

$? The return value of executing the previous instruction

Variables in shell

* any string

? An arbitrary character

[abc] one of a, b, c

[aMuthn] any character from a to n

Four, several special characters represent

\ b return

\ C print a line without a newline character, which we often use.

\ f Page change

\ r enter

\ t tabulation

\ v Vertical tabulation

\\ backslash itself

Fifth, judge the attributes of the document

Format:-operator filename

Return 1 if there is a file in-e, otherwise return 0

The-r file is readable and returns 1, otherwise 0

The-w file can be written to return 1, otherwise it returns 0

-x file executable returns 1, otherwise returns 0

-o file belongs to the user, return 1, otherwise return 0

-z file length 0 returns 1, otherwise returns 0.

The-f file returns 1 for the normal file, otherwise 0

Returns 1 if the-d file is a directory file, otherwise 0

VI. Test string

String 1 = string 2 is true when two strings are equal

String 1! = string 2 is true when two strings are not equal

-n string is true when the length of the string is greater than 0

-z string is true when the length of the string is 0

String is true when the string string is not empty

7. Test the relationship between two integers

The number 1-eq number 2 is true when two numbers are equal

The number 1-ne number 2 is not equal to true

Number 1-gt number 2 number 1 is greater than number 2 is true

Number 1-ge number 2 number 1 greater than or equal to number 2 is true

Number 1-lt number 2 number 1 is less than number 2 is true

Number 1-le number 2 number 1 less than or equal to number 2 is true

VIII. Logic test

-a vs.

-o or

! Non

The special characters in shell are

1. $dollar sign

2.\ backslash

3. `reverse quotation marks

4. "double quotation marks

5, *,?, [,]

Let me list them one by one.

I. $symbol

1. Echo $? Shows the exit status of the previous instruction

2. Echo "$?" The effect is the same as above.

3. Echo'$?' It shows $?

4. Echo\ $ It shows $?

5. Echo "\ $?" It shows $?

You may have seen that the $symbol has a special meaning in double quotation marks, while double quotation marks do not work on the $symbol, while single quotation marks can mask the special meaning of the special character so that it can be displayed as the character itself. the backslash can also shield the special meaning of the special character, making the special character lose the special meaning.

2.\ backslash

The function of the backslash is to mask the special meaning of the special symbol character so that it is still the original character.

Aban 1234

Echo\ $A displays as $An if you don't add\ it will show as 1234

Echo\ `display as `

Echo\ "appears as double quotation marks

Echo\\ is displayed as\

3. `Backquotation marks

The function of the backquote is to replace the command and execute the string in the backquote as a command. When programming with shell, we often use to assign the execution result of the system command to a variable.

A = `date`

Echo $A does not show date, but the time string at that time.

For example, there is a file A that reads as follows

ABCDEFG

1234456

Abcdefg

B = `cat A | grep 234` #

Retrieve the line in file A that contains the string 234

Echo $B will be displayed as 1234456

What will echo "$B" show?

What will echo "\ $B" show? Readers try it for themselves.

4. Double quotation marks

There are some special characters in the system, in order to avoid citing these special characters, we often use double quotation marks or single quotation marks to quote these special characters, so that they do not have a special meaning.

However, some special characters still have a special meaning in quotation marks, and double quotation marks do not work. The first four special characters listed in this article are still special characters in double quotes. 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 is.

Echo ""

Echo "$"

Echo "\"

Echo "`"

The above is not what you expect, because double quotation marks have no effect on them, so you can only output the prototype of these special characters.

Echo'"

Echo'$'

Echo'\

Echo''

Or

Echo "\"

Echo "\ $"

Echo "\"

Echo "\ `"

Will be displayed as "$\ `, respectively.

5. Other special characters

You have noticed that except for the first four special characters, I put all the other special characters together, because the first four special characters still have special meaning in double quotation marks, so take them out separately. If you want to output the prototype of these special characters, you can use double quotation marks or single quotation marks to lose their special meaning.

, *,?, [,] has a special meaning for shell, but you can enter these prototypes in double quotes.

1. If

Conditional statement

Format:

If conditional expression

Then # executes the following statement when the condition is true

Command list

Execute the following statement when else # is false

Command list

Fi

If statements can also be nested to use

If

Conditional expression 1

Then

If conditional expression 2

Then

Command list

Else

If conditional expression 3

Then

Command list

Else

Command list

Fi

Fi

Else

Command list

Fi

You can nest an if statement in multiple layers. Be sure to indicate the end of the layer condition with a fi, or it will cause syntax errors. The examples mentioned above are as follows:

Here we will first talk about the command test used in a conditional 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 with else if

Then

(cd

$1bot LPR $1)

Else

Echo "$1 is not a file or directory"

Fi

I wonder if you understand what the above example means?

If we save this example as prfile now

Chmod + x prfile

Execute the procedure just now

. / prfile aaa

This example is to check whether the parameter you entered is a file, if so, print if it is a directory, change to a directory and then print if it is neither a file nor a directory.

2. Multiple conditional test statement case

Format:

Case string in

Mode) command list

Mode) command list

....

Esac

Multiple conditional statements are based on case

Start with esac and end with multiple condition list functions to test whether the string matches the pattern in it. Execute the command list pattern in it, or the * sign indicates any string, and the last key in each pattern;; double quotation marks, otherwise a syntax error will occur.

Examples are as follows:

Case $1 in

* .c)

Cc $1

* .txt)

Lpr $1

*)

Echo "unknown type"

Esac

If you save the above in the file abc

Chmod + x abc

Execute. / abc a.c

The file a.c will be compiled

Execute. / abc readme.txt will send the file through the printer

If I change the above, will you know the result of its implementation?

Case $1 in

*)

Cc $1

* .txt)

Lpr $1

* .c)

Echo

"unknown type"

Esac

one。 While cycle

While command format

While condition table

Do

Command list

Done

Execution process

Shell first executes the condition table. If the exit state of the last statement of the condition table is zero, the command table in the shield ring is executed. After execution, the condition table is checked. If the exit state is zero, the condition table will continue to execute, and so on until the exit status of the last statement of the condition table is non-zero.

If the exit status is zero, the condition is true True.

For example, if the contents of the shell file are as follows:

Sum=0

ITunes 0

While true # true is the keyword of the system.

Do

I = `expr $I + 1`

Sum= `expr $Sum + $i`

If [$I = "100"]

Then

Break

Fi

Done

Echo $I $Sum

The last thing this program shows is

100 5050

The operation of this program is to add up 1 to 100.

Let's change the program again.

Sum=0

ITunes 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 the above.

Until can also be used as a test condition in this loop

It is exactly the opposite of the condition of the while test, that is, the statement in the body of the loop will continue to be executed when the condition is false, otherwise it will exit the body of the loop. The following example is also used.

Sum=0

ITunes 0

Until [$I = "100"]

Do

I = `expr $I + 1`

Sum= `expr $Sum + $i`

Done

Echo $I $Sum

A loop when I is not equal to 100 is a loop when the condition is false, otherwise it exits, and the first example is when I is not equal to 100.

The time loop, that is, the loop when the test condition is true.

II. For cycle

Command format:

For variable in name list

Do

Command list

Done

The name list here is a list of strings separated by spaces. When shell executes the for loop, it takes a string from the name list and assigns it to the loop variable as the value of the variable.

When writing a for statement, you can also omit the in name list, which means replacing the name list with the current position parameter.

For example, there are two directories on 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 directory have just been modified. Now I forget which files I just changed.

So I rely on the known sequence code to find it.

The procedure is as follows:

For File in a1 a2 a3 a4 a5

Do

Diff aa/$File bb/$File

Done

Here's another example of a list without names.

For

File

Do

Echo $Filw

Done

The contents of the file are saved in a.sh and can be executed

The command line when we execute this shell program is as follows:

A.sh a1 a2 a3 a4 a5

The implementation results are as follows:

A1

A2

A3

A4

A5

You can see from this example that the parameters of the command line are read one by one.

three。 Loop control statement

Break

The command does not execute the statement below break in the body of the current loop to exit from the current loop.

Continue

The command is that the program ignores the following statement in this loop and executes it from the beginning of the loop.

The above is all the content of this article "what is the Common knowledge of Shell?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 268

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report