Fork me on GitHub

Shell编程03

1.if语句

if条件语句,简单来说,其语义类似于汉语中的“如果...那么...”。if语句是Linux运维人员在实际生产过程中使用的最频繁也是最重要的语句。因此,务必重视if条件语句的知识,并牢固掌握。

2.单分支

3. 双分支

4.多分支

练习题—1


[root@shell init.d]# pwd
/etc/init.d
[root@shell init.d]# cat rsyncd 
#!/bin/bash
#如果参数是start则启动start rsync --daemon
if [ "$1" = "start" ];then
    rsync --daemon
    sleep 1
    echo "rsync服务启动成功"
fi
#如果参数是stop则关闭--pkill rsync
if [ "$1" = "stop" ];then
    #查看当前rsync进程是否存在,非0则存在
    if [ `ss -lntup|grep rsync|wc -l` -eq 0 ];then
        echo "rsync处于关闭状态"
    else
        sleep 1
        pkill rsync
    fi
fi
#如果是restart则先执行stop再执行start
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@shell init.d]# ss -lntup|grep rsync
[root@shell init.d]# sh rsyncd start
rsync服务启动成功
[root@shell init.d]# ss -lntup|grep rsync
tcp    LISTEN     0      5         *:873                   *:*                   users:(("rsync",pid=10519,fd=4))
tcp    LISTEN     0      5        :::873                  :::*                   users:(("rsync",pid=10519,fd=5))
[root@shell init.d]# sh rsyncd stop
[root@shell init.d]# ss -lntup|grep rsync
[root@shell init.d]# sh rsyncd restart
rsync处于关闭状态
rsync服务重启成功
[root@shell init.d]# ss -lntup|grep rsync
tcp    LISTEN     0      5         *:873                   *:*                   users:(("rsync",pid=10538,fd=4))
tcp    LISTEN     0      5        :::873                  :::*                   users:(("rsync",pid=10538,fd=5))

#绝对路径执行方法
需要添加执行权限
[root@shell ~]# /etc/init.d/rsyncd restart
[root@shell ~]# /etc/init.d/rsyncd stop
[root@shell ~]# /etc/init.d/rsyncd start

练习题—2

判断nginx是否活着,如果没有活着就打印“邮件报警”(判断依据:curl/wget访问url)
#取状态码的值
curl -sw "%{http_code}\n" -o /dev/null 127.0.0.1
curl -I 127.0.0.1 2>/dev/null|grep -o 200


[root@shell day03]# cat zuoye03.sh
#!/bin/bash
##############################################################
# File Name: zuoye03.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
#判断nginx是否活着,如果没有活着就打印“邮件报警
curl -I 127.0.0.1 2>/dev/null|grep -o 200
if [ $? = 0 ];then
    echo Nginx服务处于运行状态
else
    echo -e "警告!\n\tNginx服务处于关闭状态"
    echo -e "一级警告:\n\tNginx服务处于关闭状态,网站可能宕机,请尽快处理!"|mail -s "Nginx服务报警" 245684979@qq.com
fi

练习题—3


5. 函数

函数名的定义规则和变量是一样:数字、字母、下划线、但是不能以数字开头

5.1 写函数的三种方法

[root@shell day04]# cat test1.sh
#!/bin/bash
##############################################################
# File Name: test1.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
#第一种方法
#定义函数,函数加载到内存
function hello(){
    echo "hello oldchang"
    # return n
}
#使用函数
hello

#第二种方法
function hello2 {
    echo "hello2 2oldboychang"
}
hello2

#第三种方法
hello3(){
    echo "hello3 3oldoychang"
}
hello3
[root@shell day04]# sh test1.sh
hello oldchang
hello2 2oldboychang
hello3 3oldoychang

可以在函数下一起调用

5.2 优先调用函数


[root@shell day04]# touch hello
[root@shell day04]# chmod +x hello 
[root@shell day04]# echo hello >hello 
[root@shell day04]# ll
total 8
-rwxr-xr-x 1 root root   6 Jul 18 10:00 hello
-rw-r--r-- 1 root root 501 Jul 18 09:57 test1.sh
[root@shell day04]# 
[root@shell day04]# source /server/scripts/day04/test1.sh 
hello oldchang
hello2 2oldboychang
hello3 3oldoychang
[root@shell day04]# hello	#优先调用了函数
hello oldchang	

5.3 函数的退出状态码

脚本的退出状态码,使用exit定义,之后脚本退出

函数的退出状态码,使用return定义,之后函数退出

[root@shell day04]# vim test1.sh 
  1 #!/bin/bash                                                                 
  2 ##############################################################
  3 # File Name: test1.sh
  4 # Version: V1.0
  5 # Author: lcx
  6 # Organization: www.oldboyedu.com
  7 ##############################################################
  8 #
  9 #
 10 function hello(){
 11     if [ -f "/etc/hosts" ];then
 12         echo "文件存在"
 13     else
 14         return 2
 15     fi
 16 }
 17 hello
 18 echo $?
[root@shell day04]# sh test1.sh 
文件存在
0

5.4 带参数的函数

[root@shell day04]# vim test2.sh 
  1 #!/bin/bash
  2 function hello {
  3     echo "hello $1"
  4 }
  5 hello $1
[root@shell day04]# sh test2.sh oldboy
hello oldboy
[root@shell day04]# vim test2.sh 
  1 #!/bin/bash
  2 function hello {
  3    echo "hello $1 $2"	#定义2个参数
  4 }                                                                           
  5 hello $1 $2
[root@shell day04]# sh test2.sh oldboy oldchang
hello oldboy oldchang

5.5 位置函数

[root@shell day04]# cat test3.sh 
#!/bin/bash
function hello {
   echo "hello $*"
}
hello $# $1 "$*"
[root@shell day04]# sh test3.sh 1 2 3
hello 3 1 1 2 3

第二种方法,也是最常用的方法

[root@shell day04]# cat test3.sh 
#!/bin/bash
function hello {
   echo "脚本名称: $0"
   echo "函数的参数个数: $#"
   echo "第一个参数: $1"
   echo "显示所有参数: $*"
   echo "执行状态码: $?"
}
hello $*
[root@shell day04]# sh test3.sh 1 2 3 4 5
脚本名称: test3.sh
函数的参数个数: 5
第一个参数: 1
显示所有参数: 1 2 3 4 5
执行状态码: 0

作业:将nginx启动脚本修改为函数格式


6.case语句



6.1 case条件语句练习

[root@shell day04]# cat case1.sh
#!/bin/bash
##############################################################
# File Name: case1.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
case "$1" in
    "start"|"START")
        echo "已启动"
        ;;
    "stop"|"STOP")
        echo "已停止"
        ;;
    *)
        echo "你tm说的啥"
esac
 [root@shell day04]# sh case1.sh start
已启动
[root@shell day04]# sh case1.sh stop
已停止
[root@shell day04]# sh case1.sh xxx
你tm说的啥
[root@shell day04]# sh case1.sh START
已启动
[root@shell day04]# sh case1.sh STOP
已停止

练习题

6.2 练习

6.2.1 使用case判断是数字还是字母

编写脚本,使用单个字符进行传参,使用case判断是数字还是字母

[root@shell day04]# cat case2.sh 
#!/bin/bash
##############################################################
# File Name: case1.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
case "$1" in
    [0-9])
        echo "这是数字$1"
        ;;
    [a-Z])
        echo "这是字母$1"
        ;;
    *)
        echo "你tm输的啥"
        exit
esac

[root@shell day04]# sh case2.sh v
这是字母v
[root@shell day04]# sh case2.sh A
这是字母A
[root@shell day04]# sh case2.sh 5
这是数字5
[root@shell day04]# sh case2.sh 0
这是数字0
6.2.2 将” 打印菜单修改成case或if语句

2、 将” 打印菜单,按照选择项一键安装不同的web服务”脚本,将其中的条件表达式修改成case或if语句

#case语句

[root@shell day04]# cat web.sh 
#!/bin/bash
##############################################################
# File Name: zuoye02.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
#检测脚本是否存在且非空,是否可执行
file1=`[ -s /server/scripts/lamp.sh ]&& echo ture||echo fluse`
file2=`[ -s /server/scripts/lnmp.sh ]&& echo ture||echo fluse`
exec1=`[ -x /server/scripts/lamp.sh ]&& echo ture||echo fluse`
exec2=`[ -x /server/scripts/lnmp.sh ]&& echo ture||echo fluse`
case "$file1" in
    ture)
        echo -e "\033[32mlamp.sh脚本文件存在且非空\033[0m"
        ;;
    *)
        echo -e "\033[31mlamp.sh脚本文件不存在或是一个空文件!!!\033[0m"
    esac
case "$exec1" in
    ture)
        echo -e "\033[32mlamp.sh脚本文件可以执行\033[0m"
        ;;
    *)
        echo -e "\033[31mlamp.sh脚本文件不可执行!!!\033[0m"
    esac
case "$file2" in
    ture)
        echo -e "\033[32mlnmp.sh脚本文件存在且非空\033[0m"
        ;;
    *)
        echo -e "\033[31mlnmp.sh脚本文件不存在或是一个空文件!!!\033[0m"
    esac
case "$exec2" in
    ture)
        echo -e "\033[32mlnmp.sh脚本文件可以执行\033[0m"
        ;;
    *)
        echo -e "\033[31mlnmp.sh脚本文件不可执行!!!\033[0m"
esac
#打印菜单,按照选择项一键安装不同的web服务
cat << EOF
    `echo -e "\033[35m (1).[install lamp]\033[0m"`
    `echo -e "\033[36m (2).[install lnmp]\033[0m"`
    `echo -e "\033[33m (3).[exit]\033[0m"`
EOF

read -p "please input the num you want:" num
#安装lamp与lnmp环境
case "$num" in
    1)
        echo start installing LAMP
        sleep 1
        /server/scripts/lamp.sh
        exit
        ;;
    2)
        echo start installing LNMP
        sleep 1
        /server/scripts/lnmp.sh
        exit
        ;;
    3)
        echo 即将退出脚本...
        sleep 1
        exit
        ;;
    *)
        echo Input Error输入不识别!,启动自毁程序...
        sleep 2
        echo 再见人类......
        sleep 2
        exit
esac

#if语句

[root@shell day04]# cat /server/scripts/day03/zuoye02.sh 
#!/bin/bash
##############################################################
# File Name: zuoye02.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
#检测脚本是否存在且非空,是否可执行
file1=`[ -s /server/scripts/lamp.sh ]&& echo ture||echo fluse`
file2=`[ -s /server/scripts/lnmp.sh ]&& echo ture||echo fluse`
exec1=`[ -x /server/scripts/lamp.sh ]&& echo ture||echo fluse`
exec2=`[ -x /server/scripts/lnmp.sh ]&& echo ture||echo fluse`
if [ $file1 = "ture" ];then
        echo "lamp.sh脚本文件存在且非空"
    else 
        echo "lamp.sh脚本文件不存在或是一个空文件!!!"
    fi
if [ $exec1 = "ture" ];then
        echo lamp.sh脚本文件可以执行
    else
        echo lamp.sh脚本文件不可执行!!!
    fi
if [ $file2 = "ture" ];then
        echo lnmp.sh脚本文件存在且非空
    else
        echo lnmp.sh脚本文件不存在或是一个空文件!!!
    fi
if [ $exec2 = "ture" ];then
        echo lnmp.sh脚本文件可以执行
    else
        echo lnmp.sh脚本文件不可执行!!!
        exit
    fi
#打印菜单,按照选择项一键安装不同的web服务
cat << EOF
    `echo -e "\033[35m (1).[install lamp]\033[0m"`
    `echo -e "\033[36m (2).[install lnmp]\033[0m"`
    `echo -e "\033[34m (3).[exit]\033[0m"`
EOF

read -p "please input the num you want:" num
#安装lamp环境
if [ $num = "1" ];then
    echo start installing LAMP
    sleep 1
    /server/scripts/lamp.sh
    exit
elif [ $num = "2" ];then
    echo start installing LNMP
    sleep 1
    /server/scripts/lnmp.sh
    exit
elif [ $num = "3" ];then
    echo 即将退出脚本...
    sleep 1
    exit
elif [ $num != "{1..3}" ];then
    echo Input Error输入不识别!,启动自毁程序...
    sleep 2
    echo 再见人类......
    sleep 2
    exit
fi
6.3 将Nginx服务改为case方式+函数方式

3、 将Nginx服务管理脚本改为case方式+函数方式,并通过systemctl进行控制启动停止

先将nginx编译好

yum install -y gcc-c++ pcre-devel zlib-devel	#下载依赖
tar xf nginx-1.17.1.tar #解压到当前目录
cd nginx-1.17.1/	#切换到解压后的目录下
./configure --prefix=/usr/local/nginx	#路径随意
make && make install
[root@shell ~]# ll /usr/local/nginx/sbin/nginx 
-rwxr-xr-x 1 root root 3830336 Jul 18 09:19 /usr/local/nginx/sbin/nginx
[root@shell ~]# cd /etc/init.d/
[root@shell init.d]# touch nginx
[root@shell init.d]# chmod +x nginx

if方式+函数方式

[root@shell init.d]# vim nginx	#
#!/bin/bash
##############################################################
# File Name: nginx.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
if [ "$1" = "start" ];then
    nginx
    echo "nginx服务启动成功"
fi
if [ "$1" = "stop" ];then
#查看当前nginx进程是否存在,非0则存在
    if [ `ss -lntup|grep nginx|wc -l` -eq 0 ];then
        echo "nginx处于关闭状态"
    else
        pkill nginx
    fi
fi
if [ "$1" = "restart" ];then
#先关闭
    if [ `ss -lntup|grep nginx|wc -l` -eq 0 ];then
        echo "nginx处于关闭状态"
    else
        pkill nginx
        sleep 1
    fi
    nginx
    echo "nginx服务重启成功"
fi

case方式+函数方式



#为nginx添加systemd启动文件
[root@shell init.d]# cd /usr/lib/systemd/system
[root@shell system]# cat nginx_oldboy.service 
[Unit]
Description=nginx service
After=nginx_oldboy.target

[Service]
Type=forking
ExecStart=/etc/init.d/nginx start
ExecReload=/etc/init.d/nginx restart
ExecStop=/etc/init.d/nginx stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
#检查是否可以启动关闭重启
[root@shell system]# ss -lntup|grep nginx
[root@shell system]# systemctl start nginx_oldboy.service 	#启动
[root@shell system]# ss -lntup|grep nginx
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=15894,fd=6),(nginx",pid=15893,fd=6))
[root@shell system]# systemctl stop nginx_oldboy.service 	#关闭
[root@shell system]# ss -lntup|grep nginx
[root@shell system]# systemctl restart nginx_oldboy.service 	#重启
[root@shell system]# ss -lntup|grep nginx
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=15926,fd=6),(nginx",pid=15925,fd=6))
6.4 使用case选择水果,打印水果名及其颜色

1、 使用case选择水果,并打印水果名及其颜色

#给到的要求与条件
  1.apple
  2.pear
  3.banana
  4.blueberry
其中,颜色表达式分别为
31m 红色
32m 绿色
33m 黄色
34m 蓝色
使用方式为
[root@oldboy-node101 ~]# echo -e "\E[1;31m我是红色 \E[0m"
我是红色 
[root@oldboy-node101 ~]# echo -e "\E[1;32m我是绿色 \E[0m"
我是绿色 
[root@oldboy-node101 ~]# echo -e "\E[1;33m我是黄色 \E[0m"
我是黄色 
[root@oldboy-node101 ~]# echo -e "\E[1;34m我是蓝色 \E[0m"
我是蓝色
1)打印菜单
2)使用read,显示一句话“选择你喜欢的水果”
3)使用case判断数字是【1|2|3|4】
4)如果都不符合,报错退出

7.while循环

7.1 while语法

7.2 while按行读入文件

练习
处于学习阶段,欢迎补充更完美的答案

1.while循环从1加到100,然后再额外使用两种方式计算

2.while循环读入数据,计算文件内的年龄平均值

3.将第6章的练习1,改为死循环的形式。菜单中加入选项exit,只有输入exit才能退出

1.while循环从1加到100,然后再额外使用两种方式计算

[root@shell day04]# cat while3.sh
#!/bin/bash
##############################################################
# File Name: while3.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
#while循环从1加到100,然后再额外使用两种方式计算
a=0
while [ $a -lt 100 ];do
    let a++
    echo $a
done
[root@shell day04]# sh while3.sh
1
2
3
4
...
100
#第二种
[root@shell day04]# echo "(1+100)*100/2"|bc
5050

2.while循环读入数据,计算文件内的年龄平均值

[root@shell day04]# cat age.txt 
jiaojiao 20
yingqian 20
jiangyuan 25
xiaolu 30
xuxu 18
dingsheng 38
lili 5
[root@shell day04]# cat while4.sh 
#!/bin/bash
##############################################################
# File Name: while3.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
#计算平均年龄,文件是/server/scripts/age.txt
#a=echo `cat age.txt|awk '{print $2}'`
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@shell day04]# sh while4.sh age.txt
22

3.将第6章的练习1,改为死循环的形式。菜单中加入选项exit,只有输入exit才能退出

[root@shell day04]# cat caidan.sh 
#!/bin/bash
#打印菜单,按照选择项选择你喜欢的人物
prefix="I guess you like "
#菜单
while true ;do
cat << EOF
    `echo -e "\033[32m 1.棋王\033[0m"`
    `echo -e "\033[33m 2.女神\033[0m"`
    `echo -e "\033[34m 3.娘炮\033[0m"`
    `echo -e "\033[35m 4.铁人\033[0m"`
    `echo -e "\033[36m 5.退出\033[0m"`
EOF
read -p "please input the num you like: " num
if [ $num = "1" ];then
        echo "$prefix"黑嘉嘉
    elif [ $num = "2" ];then
        echo "$prefix"高圆圆
    elif [ $num = "3" ];then
        echo "$prefix"打篮球的那个蔡徐坤? Are You OK??
    elif [ $num = "4" ];then
        echo "$prefix"鹿晗...
    elif [ $num = "5" ];then
        echo "即将退出..."
        sleep 1
        exit
    else
        echo "输入错误!请重新输入"
    fi
done


7.3 练习

1、猜数字游戏:

首先让系统随机生成一个数字,给这个数字定一个范围,让用户输入猜的数字,对输入的数字进行判断,如果不符合要求,就给与高或低的提示,猜对后给出猜对用的次数,请用while语句实现。

image.png

echo $(($RANDOM%99+1)) 0到100随机数字

[root@shell lianxi]# cat 2.1.sh 
#!/bin/bash
##############################################################
# File Name: 2.1.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
#定义输入的次数
total=0
#定义字符集
export LANG="zh_CH.UTF-8"
#生成0到100随机数字并清除
num=$((RANDOM%101))
echo "生成随机数为$num"
echo "============================"
usleep 100000
clear
echo "客官~猜一猜我奶奶多大年龄了?
      请输入[1-100]的整数"
#定义检测输入值的函数age

age(){
    read -p "客官~请输入你猜的年龄: " num1
    #判断是否为整数
    expr $num1 + 1 &>/dev/null
    if [ $? -ne 0 ];then
        echo "(⊙o⊙)…输入格式错了呢~客官"
        age
    fi
}
#定义输入值反馈函数
point(){
    #调用total累计客官输入次数
    ((total++))
    if [ $num1 -eq $num ];then
        echo "客官~您真是个天才!恭喜您获得奶奶一车"
        echo "我奶奶今年${num}岁"
        echo 一共猜了${total}次
        exit 0
    elif [ $num1 -gt $num ];then
        echo "客官~我奶奶老年轻了!您猜的太大了"
        echo "再来一次!加油~: "
        age
    elif [ $num1 -lt $num ];then
        echo "客官~我奶奶比我大!您猜的太小了"
        echo "再来一次!加油~"
        age
    fi
}
#定义循环此脚本函数,猜对年龄后退出,猜错则从point函数中调用age函数继续猜
main(){
    age
    while true
    do
        point
    done
}
main
[root@shell lianxi]# sh 2.1.sh 
生成随机数为58
============================
客官~猜一猜我奶奶多大年龄了?
      请输入[1-100]的整数
客官~请输入你猜的年龄: 50
客官~我奶奶比我大!您猜的太小了
再来一次!加油~
客官~请输入你猜的年龄: 80
客官~我奶奶老年轻了!您猜的太大了
再来一次!加油~
客官~请输入你猜的年龄: 60
客官~我奶奶老年轻了!您猜的太大了
再来一次!加油~
客官~请输入你猜的年龄: 55
客官~我奶奶比我大!您猜的太小了
再来一次!加油~
客官~请输入你猜的年龄: 57
客官~我奶奶比我大!您猜的太小了
再来一次!加油~
客官~请输入你猜的年龄: 58
客官~您真是个天才!恭喜您获得奶奶一车
我奶奶今年58岁
一共猜了6次
2.使用if和while实现菜单系统信息案例:

​ h 显示命令帮助 sleep 2刷新菜单

​ f 显示登录信息 who

​ d 显示磁盘挂载 fdisk -l

​ m 查看内存使用 free -h

​ u 查看系统负载 uptime

​ q 退出程序

[root@shell lianxi]# cat menu.sh 
#!/bin/bash
##############################################################
# File Name: menu.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
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[35m [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[15分钟内系统负载为$uptime]\033[0m"
    uptime
elif [ "$NUM" = "q" ];then
    echo "bye~客官"
    exit 0
else
    echo -e "\033[31m\n\t\t\t\t输出格式不对!请重新输入编号\033[0m"
fi
done

实现效果

[root@shell lianxi]# sh menu.sh 
     [h].显示命令帮助
     [f].显示登录信息
     [d].显示磁盘挂载
     [m].查看内存使用
     [u].查看系统负载
     [q].退出程序
请输入要查询的信息编号: h

				[刷新成功!]
     [h].显示命令帮助
     [f].显示登录信息
     [d].显示磁盘挂载
     [m].查看内存使用
     [u].查看系统负载
     [q].退出程序
请输入要查询的信息编号: f

				当前在登录[3]用户
root     tty1         Jul 18 15:17
root     pts/0        Jul 18 08:34 (10.0.0.1)
root     pts/1        Jul 18 08:49 (10.0.0.1)
     [h].显示命令帮助
     [f].显示登录信息
     [d].显示磁盘挂载
     [m].查看内存使用
     [u].查看系统负载
     [q].退出程序
请输入要查询的信息编号: d

				[当前磁盘挂载情况!]

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000e3d5f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648     2508799     1048576   82  Linux swap / Solaris
/dev/sda3         2508800    41943039    19717120   83  Linux
     [h].显示命令帮助
     [f].显示登录信息
     [d].显示磁盘挂载
     [m].查看内存使用
     [u].查看系统负载
     [q].退出程序
请输入要查询的信息编号: m

				[当前内存已用11%]
              total        used        free      shared  buff/cache   available
Mem:           972M        107M        400M        7.7M        464M        688M
Swap:          1.0G          0B        1.0G
     [h].显示命令帮助
     [f].显示登录信息
     [d].显示磁盘挂载
     [m].查看内存使用
     [u].查看系统负载
     [q].退出程序
请输入要查询的信息编号: u

				[15分钟内系统负载为0.05]
 20:14:06 up 11:42,  3 users,  load average: 0.00, 0.01, 0.05
     [h].显示命令帮助
     [f].显示登录信息
     [d].显示磁盘挂载
     [m].查看内存使用
     [u].查看系统负载
     [q].退出程序
请输入要查询的信息编号: q
bye~客官
[root@shell lianxi]# 
3、while循环批量创建10个用户,并通过传参方式传入密码123

创建用户:

useradd test1

添加密码:

echo 123 | passwd --stdin test1

批量创建

seq 1 10

[root@shell lianxi]# cat user.sh
#!/bin/bash
##############################################################
# File Name: user.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
#while循环批量创建10个用户,并通过传参方式传入密码123
i=0
user=0
read -p "请为批量创建的用户添加密码: " passwd
#while [ $user -lt 10 ] ;do
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@shell lianxi]# sh user.sh
请为批量创建的用户添加密码: 123A
useradd: user 'test01' already exists
Changing password for user test01.
passwd: all authentication tokens updated successfully.
useradd: user 'test02' already exists
Changing password for user test02.
passwd: all authentication tokens updated successfully.
...
..

[root@shell lianxi]# tail /etc/passwd
test01:x:1002:1002::/home/test01:/bin/bash
test02:x:1003:1003::/home/test02:/bin/bash
test03:x:1004:1004::/home/test03:/bin/bash
test04:x:1005:1005::/home/test04:/bin/bash
test05:x:1006:1006::/home/test05:/bin/bash
test06:x:1007:1007::/home/test06:/bin/bash
test07:x:1008:1008::/home/test07:/bin/bash
test08:x:1009:1009::/home/test08:/bin/bash
test09:x:1010:1010::/home/test09:/bin/bash
test10:x:1011:1011::/home/test10:/bin/bash
4. 用case和while循环批量删除用户[y|n]
#暂时用while加if写出来了,此题没有思路
[root@shell lianxi]# cat userdel.sh 
#!/bin/bash
##############################################################
# File Name: user.sh
# Version: V1.0
# Author: lcx
# Organization: www.oldboyedu.com
##############################################################
#case方法批量删除用户
#判断是否为root用户
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
don
5. while循环通过文本来直接创建用户和密码,如果用户存在或者创建失败给予提示

posted @ 2019-08-19 09:51  lcx1997  阅读(92)  评论(0编辑  收藏  举报