千家信息网

第二十三章 SHELL脚本-CENTOS7.5知识

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,shell脚本(三)一. 使用if条件语句案例:单分支IF1).根分区已用空间>80%则报警,否则不执行任何操作#!/bin/bashecho "===Check disk usage...==="D
千家信息网最后更新 2026年10月31日第二十三章 SHELL脚本-CENTOS7.5知识


shell脚本(三)

一. 使用if条件语句

案例:

单分支IF

1).根分区已用空间>80%则报警,否则不执行任何操作

#!/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,结果为ping该主机,若通,则显示up,否则显示down

#!/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


练习

#!/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]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,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).判断httpd是否已经启动,若已启动,则提示已运行,否则启动httpd服务

systemctl status httpd

if [ $? -eq 0 ]

then

echo "Service httpd is running."

else

Systemctl start httpd

fi



双分支IF

4).判断/tmp目录下是否有win目录,若不存在则创建目录,存在不执行任何操作

#!/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 "此目录win已经存在了。"

fi


多分支示例:

脚本可互动输入分数,并判断分数在90-100之间,判断为优秀,60-89之间为合格,59-40以下其他为不及格努力;39以下复读,其它输入为非法输入。

#!/bin/bash

echo "=======Test========"

read -p "请输入你的成绩" Source

if [ $Source -gt 100 ] ; then

echo "输入错误"

elif [ $Source -lt 0 ] ; then

echo "输入错误"

elif [ $Source -ge 90 ] ; then

echo "优秀"

elif [ $Source -ge 60 ] ; then

echo "及格"

elif [ $Source -ge 40 ] ; then

echo "努力"

else

echo "复读";

fi


二、循环语句:

1、FOR循环写法

for((i=1;i<=10;i++));

for i in `seq 10`

for i in {1..10}


#!/bin/bash

for i in {1..10}

do

echo "$i"

done

#!/bin/bash

for i in $*

do

echo "$i"

done

for i in {30..39}

do

echo $i

done


vim num.txt

#!/bin/bash

for i in `cat num.txt`

do

echo "$i"

done


#!/bin/bash

ipnet='172.18.11.'

for IPaddress in {1..254}

do

ping -c 1 $ipnet$IPaddress &> /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 <=9; i++ ))

do

for (( j=1; j <= i; j++ ))

do

let "chengji = i * j"

echo -n "$i*$j=$chengji "

done

echo ""

done


2、while循环写法

如:

i=1

while [ $i -lt 10 ]

do

echo $i

let i++

done



#!/bin/bash

echo "=======Test score========"

while true ;

do

read -p "请输入你的成绩: " Source

if [ "$Source" = "exit" ] ; then

exit

elif [[ "$Source" != [0-9]* ]] ; then

echo "err,you input character."

elif [ $Source -gt 100 ] ; then

echo "输入错误"

elif [ $Source -lt 0 ] ; then

echo "输入错误"

elif [ $Source -ge 90 ] ; then

echo "优秀"

elif [ $Source -ge 60 ] ; then

echo "及格"

elif [ $Source -ge 40 ] ; then

echo "努力"

elif [ $Source -ge 0 ] ; then

echo "复读";

else

echo "input error."

fi

done


或


#!/bin/bash

#writed by jason,fenshu.

clear

echo "====Check fenshu...===="

while true

do

read -p "input your score:" Source

expr $Source + 1 &>/dev/null

if [ ! $? -eq 0 ] ; then

echo "error,wrong.You must input number."

elif [ "$Source" -gt 100 ] ; then

echo "Number too big."

elif [ "$Source" -lt 0 ] ; then

echo "Number too small."

elif [ "$Source" -ge 90 ] ; then

echo "You are great."

elif [ "$Source" -ge 60 ] ; then

echo "You are just so so ."

elif [ "$Source" -ge 40 ] ; then

echo "No pass,try your best."

elif [ "$Source" -ge 0 ] ; then

echo "You must go home."

else

echo "You input err,try again."

fi

done


或者


#!/bin/bash

echo "=======Test score========"

while true ;

do

read -p "请输入你的成绩: " Source

if [ "$Source" = "exit" ] ; then

exit

else

if [[ "$Source" =~ ^[1-9][0-9]{0,2}$ ]] ; then

if [ $Source -gt 100 ] ; then

echo "输入错误"

elif [ $Source -lt 0 ] ; then

echo "输入错误"

elif [ $Source -ge 90 ] ; then

echo "优秀"

elif [ $Source -ge 60 ] ; then

echo "及格"

elif [ $Source -ge 40 ] ; then

echo "努力"

elif [ $Source -ge 0 ] ; then

echo "复读";

else

echo "input number error."

fi

else

echo "error,not number."

fi

fi

done



补充

循环结构中数值增量需要与let合作,如let i++

i++ 等同于 i=i+1

i+=1 等同于 i=i+1

i+=2 i=i+2

i-- i=i-1

i-=2 i=i-2

i*=2 i=i*2


作业:用脚本完成

1.公司要求新进一批实习生,要为他们建立用户名shixi01 ---- shixi10,要求所有人的密码初始都为great123。

2.用FOR循环写九九乘法表。

3.用for及while结构分别完成扫描本网段址的脚本

输入 错误 脚本 循环 优秀 目录 分支 成绩 之间 写法 分数 结构 语句 主机 乘法 乘法表 公司 增量 实习生 密码 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网金融科技 广州淘业互联网络科技 互联网是第几次科技革命的成果 互联网科技的发展 狮子座互联网科技 互联网科技媒体 善班互联网科技有限公司 唐信互联网科技 大连 国外互联网科技网站 华中科技有哪些互联网大佬 宁夏科技产业互联网案例 洛阳昕诺飞互联网科技有限公司 允成互联网科技有限公司融资 翼展科技 首家互联网医院 佛山市拉法互联网科技有限公司 南博望互联网科技有限公司 互联网科技股为什么大涨 新氧科技和美国互联网有关系吗 互联网科技平台产品运营 互联网科技自媒体灵感 厦门中拓互联网络科技有限公司 关于新基建和互联网科技 互联网金融 科技金融 天津摩尔互联网科技 互联网科技美容 老年人脱离互联网科技的作文 互联网科技研发水平 互联网科技如何创业 沐思想互联网科技有限公司 山东蛋芒互联网科技有限公司
0