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

Chapter 23 SHELL script-CENTOS7.5 knowledge

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

Share

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

Shell script (3)

one。 Use if conditional statements

Case study:

Single branch IF

1)。 If the used space of the root partition is more than 80%, alarm will be given, otherwise no action will be performed.

#! / bin/bash

Echo "= Check disk usage...==="

DISKU=df-h | awk'/ vda1/ {print $5}'| awk-F%'{print $1}'

If [$DISKU-gt 10]; then

Echo "warning,Disk vda1 is over 90 usages."

Echo "warning,Disk vda1 is over 90 usages." > / var/log/diskusage.log

Else

Echo "disk vda1 is normal.Good luck."

Fi

2)。 IP is specified when the script is executed, and the result is ping the host. If the host is connected, up is displayed, otherwise down is displayed.

#! / bin/bash

Ping-c2 172.18.199.10 & > > / dev/null

If [$?-eq 0]

Then

Echo "The IP $1 is up."

Else

Echo "The IP $1 is down."

Fi

Practice

#! / bin/bash

# writed by jason.check ip.

Clear

Echo = Check IP up or down===

Read-p "Please input your IP address:" CKIP

If [["$CKIP" = ~ ^ [0-9] {1jue 3}\. [0-9] {1jue 3}\. [0-9] {1jue 3}\. [0-9] {1jue 3} $]; then

Echo "nice ip $CKIP"

Ping-C1-w 1$ CKIP & > / dev/null

If [$?-eq 0]; then

Echo "The ip $CKIP is up."

Else

Echo "The ip $CKIP is down."

Fi

Else

Echo "wrong ip $CKIP."

Echo "error,input wrong."

Fi

3)。 Determine whether httpd has been started, and if so, prompt that it is running, otherwise start the httpd service

Systemctl status httpd

If [$?-eq 0]

Then

Echo "Service httpd is running."

Else

Systemctl start httpd

Fi

Double branch IF

4)。 Determine whether there is a win directory under the / tmp directory, create a directory if it does not exist, and do not perform any operation

#! / bin/bash

If [!-e / tmp/win]; then

Echo "To create dir win"

Mkdir / tmp/win

Elif [- f / tmp/win]; then

Echo "To del win file"

Rm-rf / tmp/win

Echo "To create dir win"

Mkdir / tmp/win

Else

Echo "this directory win already exists."

Fi

Multi-branch example:

The script can interactively enter scores and judge them to be excellent between 90 and 100, qualified between 60 and 89, failing efforts below 59 and 40, re-reading below 39, and other inputs are illegal inputs.

#! / bin/bash

Echo "= Test="

Read-p "Please enter your grades" Source

If [$Source-gt 100]; then

Echo "input error"

Elif [$Source-lt 0]; then

Echo "input error"

Elif [$Source-ge 90]; then

Echo "excellent"

Elif [$Source-ge 60]; then

Echo "pass"

Elif [$Source-ge 40]; then

Echo "efforts"

Else

Echo "rereading"

Fi

2. Loop statement:

1. FOR cycle writing method

For ((iTune1 / dev/null)

If [$?-eq 0]; then

Echo "This host $ipnet$IPaddress is up."

Echo "This host $ipnet$IPaddress is up." > > / tmp/ping-18net.log

Else

Echo "This host $ipnet$IPaddress is down."

Fi

Done

Echo "Net18 ping over."

#! / bin/bash

For ((I = 1; I)

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

Network Security

Wechat

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

12
Report