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

Sorting out the basic knowledge of linux shell script

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "the basic knowledge arrangement of linux shell script". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the basic knowledge arrangement of linux shell script".

Shell script:

1 、 #! Specify the shell that the script executes

2. # comments

3. Command or grammatical structure

Echo "input"

Echo output blank line

Execute:

1. Shell script

Sh example

2. Give shell script execution permission

Chmod upright x example

Case: example sysinfo.sh

3abc x cannot start with a number

Abc3

ABC3

Environment variables-uppercase

The command execution result assignment variable uses the command substitution character ``

PATH=$PATH:/root/shell.example

Ls-l / etc/inittab / etc/fstab / etc/services

$0 $1 $2 $3

$0Mutual 9

$* / etc/inittab / etc/fstab / etc/services

$# 3

$$PID

$? The command returns a non-zero value of 0

Case: special.var

Read case: read

The expr operator is preceded by a space, and the multiplication sign * is preceded by an escape character

Case: expr

Test

1. Test whether the string is equal and empty

2. Integer test is equal, greater than, and less than

3. File test file type, file permissions, file existence, file size

Single cycle: if/then

If test condition

Then

Perform an operation

Fi

APACHE= `/ usr/bin/pgrep httpd`

If ["$APACHE" = ""]

Then

/ etc/rc.d/init.d/httpd start

Fi

The sh-x script displays + or + + comments during execution

* / 2 12-14 * 3-6 recorder 9-12 1-5 / root/shell-example/apache.test

Two-branch cycle: if/else

If test statement

Then

True: operation

Else

False: operation

Fi

Case: test.apache autobak.sh

Tar-cf new filename directory

Multi-branch cycle: if/elif

Case: if_else determines the file type

Conditional connection-o logic or or-a logic and and

Exit exit statement

If ["$#"! = 2]

Then

Echo "MUST two parm!"

Fi

Echo "this is a test"

Echo $1 $2

Sh exit first

Condition is true exit exit 0 condition is false exit exit 1 is not 0

Add exit statement sh exit first

For cycle

Case: for automatically kicks the user out of the system killuser.sh

Awk-F domain delimiter 'command' default delimiter is a space

Ps-le | grep httpd | awk'{print $4}'

Awk-F:'$3 print 0 {print $1}'/ etc/passwd

Count the number of users of the system

Awk-F:'$3 > = 500 {print $1}'/ etc/passwd | grep-v nfsnobody | wc-l

Awk-F: 'length ($2) = = 0 {print $1}' / etc/shadow

Case case: case / etc/rc.d/init.d/httpd

While condition is true all the time case: while useradd.sh

The until condition is false constant cycle.

Echo password | passwd-- stdin user name

While count loop

Windows scripting will have unrecognized controllers to deal with

Dos2unix script

Thank you for your reading, the above is the content of "the basic knowledge of linux shell script collation", after the study of this article, I believe you have a deeper understanding of the basic knowledge of linux shell script collation of this problem, the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 0

*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