目录
案例2:case 脚本查看系统信息(磁盘 负载 内存 登录用户 IP 系统版本)
案例3:Nginx 启动脚本(注意脚本方式启动和关闭服务,命令行无法查看结果)
1.case
结构 变量 in
变量===名字 自己起的 脚本的传参 $1 $2
case 变量 in if [ $1 == abin ];then
模式1) echo ok
命令大礼包
;;
模式2)
命令大礼包
;;
模式3)
命令大礼包
;;
*)
echo 没有匹配到
esac
[root@web01 /server/scripts]# cat case.sh
#!/bin/sh
##create abin 2020-6-16
case $1 in
Linux)
echo "Linux is done...."
;;
Centos)
echo "Centos is done..."
;;
Mysql)
echo "Mysql is done...."
;;
*)
echo "USAGE: $0 [Linux|Centos|Mysql]"
esac
[root@web01 /server/scripts]# sh case.sh
USAGE: case.sh [Linux|Centos|Mysql]
[root@web01 /server/scripts]# sh case.sh Linux
Linux is done....
[root@web01 /server/scripts]# sh case.sh Centos
Centos is done...
[root@web01 /server/scripts]# sh case.sh Mysql
Mysql is done....
案例1:批量删除用户(if判断也可以)
1)删除用户的前缀 read -p "please input prefix:" pre
2)删除用户的数量 read -p "用户数量:" num
for i in `seq $num`
do
echo ...
done
read -p "你确定要删除以上用户吗?[y|Y|yes|n|N|no]" te
case $te in
案例1脚本:(批量删除用户脚本)
[root@web01 /server/scripts]# cat case.sh
#!/bin/sh
##create abin 2020-6-16
read -p "Please input prefix:" pre
read -p "Please input num:" num
for i in `seq $num`
do
echo $pre$i
done
read -p "确定要删除以上用户吗?[y|Y|yes|n|N|no]" re
for n in `seq $num`
do
user=$pre$n
case $re in
y|Y|yes)
id $user >/dev/null 2>&1
if [ $? -eq 0 ];then
userdel -r $user
[ $? -eq 0 ] && echo "$user del is ok"
else
echo "id: $user: no such user"
fi
;;
n|N|no)
echo "不删除,玩啥呢"
exit
esac
done
案例1环境:利用for循环增加5个用户
[root@web01 /server/scripts]# for i in `seq 5`;do useradd abin$i;done
案例1结果:
[root@web01 /server/scripts]# sh case.sh
Please input prefix:abin
Please input num:5
abin1
abin2
abin3
abin4
abin5
确定要删除以上用户吗?[y|Y|yes|n|N|no]y
abin1 del is ok
abin2 del is ok
abin3 del is ok
abin4 del is ok
abin5 del is ok
案例2:case 脚本查看系统信息(磁盘 负载 内存 登录用户 IP 系统版本)
要求使用菜单 显示
1)help帮助 打印菜单
2)显示内存使用
3)显示磁盘使用
4)显示系统负载
5)显示登录用户
6)查看IP地址
7)查看Linux-version
read -p 请输入要查看的系统信息编号:num
案例2脚本:
[root@web01 /server/scripts]# cat case-anli2.sh
#!/bin/sh
##create abin 2020-6-16
menu(){
cat<<EOF
1.help帮助
2.显示内存使用
3.显示磁盘使用
4.显示系统负载
5.显示登录用户
6.查看IP地址
7.查看Linux-version
8.退出
EOF
}
menu
while true
do
read -p "请输入要查看系统的编号:" num
case $num in
1)
clear
menu
;;
2)
free -h
;;
3)
df -h
;;
4)
uptime
;;
5)
w
;;
6)
hostname -I
;;
7)
cat /etc/redhat-release | awk '{print $(NF-1)}'
;;
8)
exit
;;
*)
echo "请输入要查看系统的编号"
esac
done
案例2结果:
[root@web01 /server/scripts]# sh case-anli2.sh
1.help帮助
2.显示内存使用
3.显示磁盘使用
4.显示系统负载
5.显示登录用户
6.查看IP地址
7.查看Linux-version
8.退出
请输入要查看系统的编号:2
total used free shared buff/cache available
Mem: 974M 130M 663M 25M 180M 658M
Swap: 1.0G 0B 1.0G
请输入要查看系统的编号:3
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_oldboyedu-root 49G 2.0G 47G 4% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 197M 108M 90M 55% /boot
tmpfs 98M 0 98M 0% /run/user/0
请输入要查看系统的编号:8
[root@web01 /server/scripts]#
案例3:Nginx 启动脚本(注意脚本方式启动和关闭服务,命令行无法查看结果)
命令行方式启动
/usr/sbin/nginx 启动命令
/usr/sbin/nginx -s stop
/usr/sbin/nginx -s reload 重新加载
重启
/usr/sbin/ngonx -s stop
sleep 2
/usr/sbin/nginx
查看状态
手动过滤出监听的端口和Nginx的PID 打印输出
[root@web01 /server/scripts]# . /etc/init.d/functions
[root@web01 /server/scripts]# action "Nginx $1 is" /bin/true
Nginx is [ OK ]
案例3脚本:
[root@web01 /server/scripts]# cat startnginx.sh
#!/bin/sh
##create abin 2020-6-16
te=$1
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
test(){
if [ $? -eq 0 ];then
action "Nginx $te is" /bin/true
else
action "Nginx $te is" /bin/false
fi
}
start(){
/usr/sbin/nginx >/dev/null 2>&1
}
stop(){
/usr/sbin/nginx -s stop >/dev/null 2>&1
}
restart(){
stop
sleep 2
start
}
reload(){
/usr/sbin/nginx -s reload >/dev/null 2>&1
}
status(){
echo "当前Nginx监听端口:`netstat -lntup|grep [n]ginx|awk '{print $4}'`"
echo "当前NginxPID为:`ps aux|grep [n]ginx|grep master|awk '{print $2}'`"
}
case $1 in
start)
start
test
;;
stop)
stop
test
;;
restart)
restart
test
;;
reload)
reload
test
;;
status)
status
test
;;
*)
echo "USAGE $0 [start|stop|reload|restart|status]"
esac
案例3结果:
[root@web01 /server/scripts]# sh startnginx.sh status
当前Nginx监听端口:
当前NginxPID为:
Nginx status is [ OK ]
[root@web01 /server/scripts]# sh startnginx.sh start
Nginx start is [ OK ]
[root@web01 /server/scripts]# sh startnginx.sh status
当前Nginx监听端口:0.0.0.0:80
当前NginxPID为:2551
Nginx status is [ OK ]
[root@web01 /server/scripts]# sh startnginx.sh stop
Nginx stop is [ OK ]
[root@web01 /server/scripts]#
案例4:jumpserver 案例
1)需要的连接服务器的IP地址
WEB1=10.0.0.7
WEB2=10.0.0.8
NFS=10.0.0.31
MYSQL=10.0.0.51
菜单
01.web 10.0.0.7
02.web02 10.0.0.8
03.Mysql 10.0.0.51
04.NFS 10.0.0.31
2)需要和服务器之间做免私钥
ssh-keygen -t dsa
ssh-copy-id -i /root/.ssh/id_dsa.pub 10.0.0.8 --此处应对菜单中主机分别分发公钥
3)如何登陆服务器
ssh root@$WEB
4)使用case语句控制
案例4脚本:
[root@web01 /server/scripts]# cat jumpserver.sh
#!/bin/sh
##create abin 2020-6-16
WEB1=10.0.0.7
WEB2=10.0.0.8
MYSQL=10.0.0.51
NFS=10.0.0.31
menu(){
cat<<EOF
1.WEB1=10.0.0.7
2.WEB2=10.0.0.8
3.MYSQL=10.0.0.51
4.NFS=10.0.0.31
5.菜单
EOF
}
menu
trap "echo "不要乱输,小心炸掉你"" INT TSTP HUP
while true
do
read -p "请输入需要连接的服务器编号或名称[1|WEB1:]" num
case $num in
1)
ssh root@$WEB1
;;
2)
ssh root@$WEB2
;;
3)
ssh root@$MYSQL
;;
4)
ssh root@$NFS
;;
5)
menu
;;
woshiyunwei) --退出后门(输入:woshiyunwei)
exit
;;
*)
read -p "请输入需要连接的服务器编号或名称[1|WEB1:]" num
esac
done
案例4结果:
[root@web01 /server/scripts]# sh jumpserver.sh
1.WEB1=10.0.0.7
2.WEB2=10.0.0.8
3.MYSQL=10.0.0.51
4.NFS=10.0.0.31
5.菜单
请输入需要连接的服务器编号或名称[1|WEB1:]2
Last login: Tue Jun 16 15:11:21 2020 from 10.0.0.7
[root@web02 ~]#
2.while循环
工作中for循环用的更多
PS:while循环行取值,for循环按字符串取值
for循环
for i in 取值列表
do
执行命令
done
while 循环
while [ 条件表达式 ]
do
echo ""
done
line(变量)
while read line
do
done<user.txt
案例5:使用while循环创建用户
案例5:使用while循环创建用户
案例5脚本:
[root@web01 /server/scripts]# cat while2.sh
#!/bin/sh
while read line --读取行
do
user=`echo $line|awk '{print $1}'`
pass=`echo $line|awk '{print $2}'`
useradd $user
echo $pass|passwd --stdin $user
done<user.txt
案例5结果:
[root@web01 /server/scripts]# sh while2.sh
Changing password for user zhangsan.
passwd: all authentication tokens updated successfully.
Changing password for user lisi.
passwd: all authentication tokens updated successfully.
Changing password for user wangwu.
passwd: all authentication tokens updated successfully.
[root@web01 /server/scripts]# su - lisi
[lisi@web01 ~]$
3.shell内置命令
exit 结束循环
continue 继续执行
break 跳出循环体 执行循环体外的命令
案例6:
#!/bin/sh
for i in `seq 10`
do
useradd test$i
if [ $? -eq 0 ];then
echo "Create $i Success"
else
exit --shell内置命令
fi
done
4.猜数字(利用for循环直接输出结果)
#!/bin/sh
ran=`echo $((RANDOM%100+1))`
echo $ran
for i in {1..100}
do
[ $i -eq $ran ] && echo "我判断的随机数是 $i"
done