Shell复习—第三天

if分支语句

1.写一个rsync的启动脚本,需求如下:
类似于/etc/init.d/rsyncd {start|stop|restart}
[root@node3 init.d]# cat rsyncd 
#!/bin/bash

if [ "$1" = "start" ]; then
        rsync --daemon
        sleep 1
        echo "rsync服务启动成功"
fi

if [ "$1" = "stop" ]; then
        if [ `ss -lntup|grep rsync|wc -l` -eq 0 ];then
                echo "rsync处于关闭状态"
        else
                sleep 1
                pkill rsync
                echo "rsync服务关闭成功"
        fi
fi

if [ "$1" = "restart" ]; then
        if [ `ss -lntup|grep rsync|wc -l` -eq 0 ];then
                echo "rsync处于关闭状态"
        else
                sleep 1
                pkill rsync
        fi
        rsync --daemon
        echo "rsync服务重启成功"
fi
[root@node3 init.d]# pwd
/etc/init.d
[root@node3 init.d]# ss -lntup |grep rsync
tcp    LISTEN     0      5         *:873                   *:*                   users:(("rsync",pid=1774,fd=4))
tcp    LISTEN     0      5      [::]:873                [::]:*                   users:(("rsync",pid=1774,fd=5))
[root@node3 init.d]# sh rsyncd stop
rsync服务关闭成功
[root@node3 init.d]# ss -lntup|grep rsync
[root@node3 init.d]# 
2.判断nginx是否活着,如果没有活着就打印“邮件报警"(判断依据:curl/wget)

#取状态码的值
[root@node3 ~]# curl -sw "%{http_code}\n" -o /dev/null 127.0.0.1
[root@node3 ~]# curl -I 127.0.0.1 2>/dev/null |grep -o 200

[root@node3 shell]# vim nginx.sh
#!/bin/bash


curl -I 127.0.0.1 2> /dev/null|grep -o 200

if [ "$?" -eq 0 ]; then
        echo "nginx服务处于运行状态"
else
        echo -e "警告!\n\tnginx服务处于关闭状态"
        echo -e "一级警告:\n\tnginx服务处于关闭状态,网站可能宕机,请尽快处理!" | mail -s "ngin
x服务器报警" admin@giaogg.cn
fi

[root@node3 shell]# sh nginx.sh 
警告!
        nginx服务处于关闭状态
3.利用安装好(自己编译)的nginx,写一个nginx的启动脚本,需求如下:
1)类似于/etc/init.d/nginxd {start|stop|restart}
2)启动方式:nginx
3)关闭方式:nginx -s stop
4)使用systemctl实现开机自启动管理

函数

[root@node3 shell]# vim fun.sh
#!/bin/bash

hello (){
        echo "脚本名称: $0"
        echo "函数的参数个数:$#"
        echo "第一个参数:$1"
        echo "显示所有参数:$*"
        echo "执行状态码:$?"
}
hello $*

[root@node3 shell]# sh fun.sh 1 2 3 4 5 
脚本名称: fun.sh
函数的参数个数:5
第一个参数:1
显示所有参数:1 2 3 4 5
执行状态码:0

case语句

1.使用case判断数字还是字母
[root@node3 shell]# vim case.sh
#!/bin/bash

case "$1" in
        [0-9]):
        echo "这是数字$1"
        ;;
        [a-Z]):
        echo "这是字母$1"
        ;;
        *):
        echo "你输的什么"
        exit    
esac

[root@node3 shell]# sh case.sh 
你输的什么
[root@node3 shell]# sh case.sh 1
这是数字1
[root@node3 shell]# sh case.sh a
这是字母a
[root@node3 shell]# sh case.sh X
这是字母X
[root@node3 shell]# sh case.sh .
你输的什么

while循环

 while按行读入文件

第一种:exec方式
exec < file
sum=0
while read line
do
	echo $line
done

第二种:cat方式
cat file | while read line 
do
	echo $line
done

第三种:<重定向输入方式
while read line
do
	echo $line
done < file
1.while循环从1加到100
[root@node3 ~]# cat while.sh 
#!/bin/bash

a=0
sum=0
while [ $a -lt 100 ];do
        let a++
        let sum=sum+a
        echo $sum
done



2.whille循环读入数据,计算文件内的平均年龄
[root@node3 ~]# cat age.txt 
jiaojiao 20
yingqian 20
jiangyuan 25
xiaolu 30
xuxu 18
dingsheng 38
lili 5

[root@node3 ~]# vim while2.sh
#!/bin/bash

sum=0
count=0

while read line
do
        age=`echo $line|awk '{print $2}'`
        let sum=${sum}+${age}
        let count++
done < $1

echo "$sum/$count" | bc
[root@node3 ~]# sh while2.sh age.txt 
22
#使用if和while实现菜单系统信息案例:
h 显示命令帮助 刷新菜单
f 显示登录信息 who
d 显示磁盘挂载 fdisk -l
m 显示内存使用 free -h
u 查看系统负载 uptime
q 退出程序


[root@node3 ~]# cat menu.sh 
#!/bin/bash

export LANG="zh_CH.UTF-8"
user=`who|wc -l`
free1=`free -h|awk 'NR==2{print $3/$2*100}'`
free2=${free1/.*/}
uptime=`uptime|awk '{print $NF}'`

while true
do
        cat <<EOF
                `echo -e "\033[32m [h].显示命令帮助\033[0m"`
                `echo -e "\033[32m [f].显示登录信息\033[0m"`
                `echo -e "\033[32m [d].显示磁盘挂载\033[0m"`
                `echo -e "\033[32m [m].查看内存使用\033[0m"`
                `echo -e "\033[32m [u].查看系统负载\033[0m"`
                `echo -e "\033[32m [q].退出程序\033[0m"`
EOF

read -p "请输入要查询的信息编号:" NUM

if [ "$NUM" = "h" ];then
        sleep 1
        clear
        echo -e "\033[32m\n\t\t\t\t[刷新成功!]\033[0m"
elif [ "$NUM" = "f" ];then
        echo -e "\033[32m\n\t\t\t\t当前在登录[$user]用户\033[0m"
        who
elif [ "$NUM" = "d" ];then
        echo -e "\033[32m\n\t\t\t\t[当前磁盘挂载情况!]\033[0m"
        fdisk -l
elif [ "$NUM" = "m" ];then
        echo -e "\033[32m\n\t\t\t\t[当前内存已用$free2%]\033[0m"
        free -h
elif [ "$NUM" = "u" ];then
        echo -e "\033[32m\n\t\t\t\t[当前系统负载为$uptime]\033[0m"
        uptime
elif [ "$NUM" = "q" ];then
        echo "bay~"
        exit 0
else
        echo -e "\033[32m\n\t\t\t\t请重新输入编号\033[0m"
fi
done
#while循环批量创建10个用户,并通过传参方式传入密码123
创建用户
useradd test1
添加密码
echo 123 | passwd --stdion tst1
批量创建
seq 1 10

[root@node3 ~]# vim user.sh
#!/bin/bash

i=0
user=0

read -p "请为批量创建的用户添加密码:" passwd

while ((i<=10))
do
        ((user=sum+i))
        ((i++))
done

seq -w $user | sed -r "s#(.*)#useradd test\1;echo $passwd|passwd --stdin test&#g"|bash

[root@node3 ~]# sh user.sh 
请为批量创建的用户添加密码:123
Changing password for user test01.
passwd: all authentication tokens updated successfully.
Changing password for user test02.
passwd: all authentication tokens updated successfully.
Changing password for user test03.
passwd: all authentication tokens updated successfully.
Changing password for user test04.
passwd: all authentication tokens updated successfully.
Changing password for user test05.
passwd: all authentication tokens updated successfully.
Changing password for user test06.
passwd: all authentication tokens updated successfully.
Changing password for user test07.
passwd: all authentication tokens updated successfully.
Changing password for user test08.
passwd: all authentication tokens updated successfully.
Changing password for user test09.
passwd: all authentication tokens updated successfully.
Changing password for user test10.
passwd: all authentication tokens updated successfully.
#用case和while循环批量删除用户
[root@node3 ~]# vim userdel.sh 
#!/bin/bash
while true ;do
if [ $UID -ne 0 ];then
    echo "必须是root用户才有删除的权限!" user
    exit 1;
fi
read -p "请输入要删除的用户: " name
user=`awk -F: '{print $1}' /etc/passwd|grep "$name"`
if [ $? = 0 ];then
    read -p "此用户是否真的要删除[y|n]: "  or
    if [ "$or" = "y" ];then
        userdel -r $user
        echo "$user用户已删除"
    elif [ "$or" = "n" ];then
        echo "返回主页..."
    else
        echo "输入有误!请重新输入"
    fi
else
    echo "${name}用户不存在"
fi
done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不想敲代码的运维

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值