跟高手学习LINUX笔记-20

第20节:条件判断语句和if的使用
本节所讲内容:
20.1 read命令键盘读取变量的值
read命令最主要是读入输入的参数
重要参数
-p:后面跟提示内容
read -p "请输入您的姓名:" $name
-t:后面跟个数字,超过此数字的秒数不输入则自动退出
read -t 5 -p "请输入您的姓名:" $name
5秒钟不输入则自动跳出
-s:不显示输入内容,常用于输入密码时
例1:
[root@node-1 scripts]# cat test-read.sh
#!/bin/bash
read -p "Please input your name:" NAME
read -p "Please input your age :" AGE

cat << eof


你的基本信息如下:
姓名: $NAME
年龄:$AGE


eof

20.2 流程控制语句if
流程控制语句if语法:
if [ 条件判断 ]
then
命令
elif
命令
else
命令
fi
在说明if之前先介绍if中比较的意义
2.1数值的比较
-eq:是否相等
-nq:是否不相等
-gt:是否大于
-lt:是否小于
-ge:是否大于等于
-le:是否小于等于
例2:
[root@node-1 scripts]# cat test2.sh
#!/bin/bash
read -p "input num1 num2 :" num1 num2
if [ $num1 -gt $num2 ]
then
echo "$num1 > $num2"
elif [ $num1 -lt $num2 ]
then
echo "$num1 < $num2"
else
echo "$num1 = $num2"
fi
2.2 字符串比较常用参数
==:等于为真
!=:不等于为真
-z:字符串的长度为零则为真
例3:
[root@node-1 scripts]# cat test3.sh
#!/bin/bash
read -p "input your name:" name
if [ $name == "root" ]
then
echo "you are super administrator"
else
echo "You are a general user"
fi
2.3文件判断常用参数
-e:文件是否存在
-f:是否为普通文件
-d:是否为文件夹
-r:是否可读
-w:是否可写
-x:是否可执行
例4:清空日志文件保留前100行
[root@node-1 scripts]# cat clear-log.sh
#!/bin/bash
#是否为root用户
if [ $USER != "root" ]
then
echo "you must be root!"
exit 0
fi
#判断文件是否存在
if [ ! -f /var/log/messages ]
then
echo "file not exists!"
echo 1
fi
tail -100 /var/log/messages > /var/log/mesg.tmp
rm -rf /var/log/messages
cat /var/log/mesg.tmp > /var/log/messages

例5
要求如下:
在服务器上创建一个名为/root/scripts/check.sh 的脚本,功能如下:
1)当运行/root/check.sh redhat,输出为 centos
2)当运行/root/check.sh centos,输出为 redhat
3)当没有任何参数或者参数不是 redhat 或者 centos 时,其错误输出产生以下的信息:
/root/scripts/check.sh redhat|centos
[root@node-1 scripts]# cat check.sh
#!/bin/bash
if [ "$1"=="redhat" ]
then
echo "centos"
elif [ "$1"=="centos" ]
then
echo "redhat"
else
echo "/root/scripts/check.sh redhat|centos"
fi
运行结果测试
[root@node-1 scripts]# sh check.sh redhat
centos
[root@node-1 scripts]# sh check.sh centos
redhat
[root@node-1 scripts]# sh check.sh aa
/root/scripts/check.sh redhat|centos
20.4 流程控制过程中复杂条件和通配符
复杂条件:
-a和&& 条件与
-o和|| 条件或
*:匹配0或多个字符
?:匹配1个字符

例6:
[root@node-1 scripts]# cat /etc/profile |less

if [ $UID -gt 199 ] && [ "/usr/bin/id -gn" = "/usr/bin/id -un" ]; then
umask 002
else
umask 022
fi
20.5 实战-几个日常用的shell脚本实战
实战脚本1:
判断主机CPU占用率
[root@node-1 scripts]# cat checkcpu.sh

#/bin/bash
#environment variable
source /etc/profile
#cpu使用情况
cpu_us=vmstat | awk ‘{print $13}‘ | sed -n ‘$p‘
cpu_sy=vmstat | awk ‘{print $14}‘ | sed -n ‘$p‘
cpu_id=vmstat | awk ‘{print $15}‘ | sed -n ‘$p‘
cpu_sum=$(($cpu_us+$cpu_sy))
if(($cpusum >= 90))
then
msg="TIME:$(date +%F
%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk ‘NR==2{print $2}‘)
MSG:CPU使用率过高!已经用了${cpu_sum}%"
echo $msg
fi
执行结果
[root@node-1 scripts]# sh checkcpu.sh
TIME:2020-04-02_23:23:17 HOSTNAME:node-1 IPADDR:192.168.26.71 MSG:CPU使用率过高!已经用了92%

实战脚本2:
判断vsftpd是否安装若未安装则安装--其他服务把名字修改一下
[root@node-1 scripts]# cat chkvsftpd.sh
#!/bin/bash
rpm -qa |grep vsftpd &>/dev/null
if [ $? -ne 0 ]
then
yum -y install vsftpd &>/dev/null && echo “vsftpd installed suecess.”
if [ $? -eq 0 ]
then
systemctl restart vsftpd && systemctl enable vsftpd
else
echo "vsftpd install failure!"
fi
fi

执行结果:
[root@node-1 scripts]# sh chkvsftpd.sh
“vsftpd installed suecess.”
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

实战脚本3:
测试指定的IP地址是否能ping 通
[root@node-1 scripts]# cat chkping.sh
#!/usr/bin/bash
read -p "Please input a ip: " ip
ping -c 3 ${ip} &>/dev/null
if [ $? -eq 0 ]
then
echo "${ip} now is up."
else
echo "${ip} is down."
fi

实战脚本4:判断是否开启80端口
[root@node-1 scripts]# cat chk80port.sh
#!/bin/bash
port=netstat -lnp | grep 80
if [ -z "port" ]; then
echo "not start service.";
exit;
fi
web_server=echo $port | awk -F‘/‘ ‘{print $2}‘|awk -F : ‘{print $1}‘
case $web_server in
httpd )
echo "apache server."
;;
nginx )
echo "nginx server."
;;

  • )
    echo "other server."
    ;;
    esac

测试结果:
[root@node-1 scripts]# sh chk80port.sh
apache server.

实战脚本5:
编译安装的httpd服务控制脚本
说明:源码安装httpd位置在/usr/local/apache目录下
#!/bin/bash
if [ $1 == start ];then
netstat -utpln |grep 80 &>/dev/null
if [ $? -eq 0 ];then
echo "httpd is started!"
exit 1
else
/usr/local/apache/bin/apachectl start &>/dev/null
echo "+/usr/local/apache/bin/apachectl start"
echo "+netstat -utpln |grep 80"
netstat -utpln |grep 80
PID=$(netstat -utpln |grep 80 |awk ‘{print $7}‘|awk -F‘/‘ ‘{print $1}‘)
echo -e "httpd is running ! PID is $PID"
fi
elif [ $1 == stop ];then
netstat -utpln |grep 80 &>/dev/null
if [ $? -ne 0 ];then
echo "httpd is stoped!"
exit 1
else
/usr/local/apache/bin/apachectl stop &>/dev/null
echo "+/usr/local/apache/bin/apachectl stop"
sleep 2
echo "+netstat -utpln |grep 80"
netstat -utpln |grep 80
if [ $? -ne 0 ];then
echo "httpd is stopping!"
fi
fi
elif [ $1 == restart ];then
netstat -utpln |grep 80 &>/dev/null
if [ $? -ne 0 ];then
echo "httpd not is started!"
/usr/local/apache/bin/apachectl start &>/dev/null
echo "+/usr/local/apache/bin/apachectl start"
sleep 2
echo "+netstat -utpln |grep 80"
netstat -utpln |grep 80
if [ $? -eq 0 ];then
echo "httpd is started!"
fi
else
/usr/local/apache/bin/apachectl stop &>/dev/null
echo "+/usr/local/apache/bin/apachectl stop"
sleep 2
/usr/local/apache/bin/apachectl start &>/dev/null
echo "+/usr/local/apache/bin/apachectl start"
sleep 2
echo "+netstat -utpln |grep 80"
netstat -utpln |grep 80
if [ $? -eq 0 ];then
echo "httpd is started!"
fi
fi
elif [ $1 == status ];then
netstat -utpln |grep 80 &>/dev/null
if [ $? -eq 0 ];then
echo "httpd is running!"
PID=$(netstat -utpln |grep 80 |awk ‘{print $7}‘|awk -F‘/‘ ‘{print $1}‘)
echo -e "httpd is running ! PID is $PID"
else
echo "httpd is not running!"
fi
else
echo "Usage: /etc/init.d/httpd start|stop|restart|status "
fi

跟高手学习LINUX笔记-20

上一篇:Ubuntu、centos系统下安装 Docker


下一篇:linux主机连接sftp报错received unexpected end-of-file from SFTP server