视频讲解在微信公众号
问渠清源
回复关键词视频
即可观看
一、shell 脚本编程
shell 在linux系统中是自带的一种语言
1.1 为什么要使用shell脚本
.py #python脚本,搭建openstack时候
.sh #shell脚本
-
可以简化日常的运维工作,从简单重复的劳动中解脱
-
为了自动化运维 需要脚本
python
shell
来编写脚本ansible
自动化
shell编程:
-
熟练使用vim vim有颜色提示,可以自动判断脚本是否写对
-
熟练使用所有linux命令
-
熟练使用三剑客 和查找工具 定位工具 排版工具 排序工具
-
有良好的逻辑编程能力
1.2 如何创建一个脚本 hello world
- 创建sh文件 以
.sh
来结尾的文件 - 编辑注释器 以
#!/bin/bash
开头 - 编写脚本
- 需要修改脚本权限
chmod 755 文件名
- 以相对路径或绝对路径来执行脚本
[root@wentan dwt]# vim hello.sh
#!/bin/bash
echo "hello,world"
[root@wentan dwt]# ll
total 7824
-rw-r--r--. 1 root root 31 Jan 20 16:10 hello.sh
[root@wentan dwt]# chmod 755 hello.sh
[root@wentan dwt]# ./hello.sh
hello,world
在shell中 使用#
来充当注释符
1.3 变量
在 shell
中定义变量不需要指定变量类型
-
位置变量 :
$1 $2 $3
-
预定义变量:
bash
已经定义好的变量$* #所有的参数 $@ #所有的字符串 $$ #当前的PID $# #参数的个数 $? #上一个命令的返回值 ,0表示成功 $UID $HOME $PATH
-
自赋值变量
[root@wentan ~]# name=wentan [root@wentan ~]# echo $name wentan
-
撤销变量
[root@wentan ~]# unset name [root@wentan ~]# echo $name
-
数组
[root@wentan ~]# group=(1 2 3 4 5) [root@wentan ~]# echo $group 1
变量的几个特点
(1)变量严格区分大小写
(2)变量名在
shell
中最好是大写的(3)变量的调用
$
谨慎使用转义字符 -
整数运算
在shell中 使用expr功能来进行计算
expr 10 + 3 #输出的是结果13 expr 10+3 #输出的是10+3
运算符前后必须加上空格! 否则输出的是字符串
-
用户输入
read -p
跟字符串 参数调用参数 $参数
1.4 运算符
1.4.1 匹配测试
一般都是和条件语句想结合的
&& #与
|| #或
1.4.2 文件测试
-d 文件名 #判断该文件是否为目录
-f 文件名 #判断该文件是否为普通文件
-g 文件名 #判断该文件是否设置了SGID
-u 文件名 #判断该文件是否设置了SUID
1.4.3 数值运算符
-eq #检测两边是否相等 相同返回true
-ne #检测两边是否相等 不等返回true
-gt #检查左边是否大于右边
-lt #检查左边是否小于右边
-ge #检查左右是否大于等于右边
-le #检查左边是否小于等于右边
1.4.4 布尔运算符
-a #与
-o #或
! #非
1.4.5 字符串运算符
= #等于
!= #不等于
1.5 循环语句
1.5.2 条件循环
if [a=b 返回值为true]
then
执行command1
elif [a>b 返回值为true]
then
执行command2
elif [a<b 返回值为true]
then
执行command3
fi
1.5.2 case 语句
case number in
number>10)
command1
;;
number =10)
command2
;;
number<10)
command3
;;
esac
1.5.3 for 语句
for (( i=1; i<10;i++))
do
command1
done
until语句
until test command1
do
command2
done
二、shell 编程实战
2.1 数值运算符
[root@wentan ~]# cd /dwt/
[root@wentan dwt]# mkdir shell
[root@wentan dwt]# cd shell/
[root@wentan shell]# vim math.sh
#!/bin/bash
a=10
b=20
if [ $a -eq $b ]
then
echo "$a -eq $b : a 等于 b"
else
echo "$a -eq $b : a 不等于 b"
fi
[root@wentan shell]# chmod 755 math.sh
[root@wentan shell]# ./math.sh
10 -eq 20 : a 不等于 b
2.2 bool 运算符
[root@wentan shell]# vim bool.sh
#!/bin/bash
a=10
b=20
if [ $a != $b ]
then
echo "$a != $b : a不等于b"
else
echo "$a == $b : a等于b"
fi
if [ $a -le 100 -a $b -gt 15 ]
then
echo "$a 小于 100 且 $b 大于 15 : 返回true"
else
echo "$a 小于 100 且 $b 大于 15 : 返回flase"
fi
[root@wentan shell]# chmod 755 bool.sh
[root@wentan shell]# ./bool.sh
10 != 20 : a不等于b
10 小于 100 且 20 大于 15 : 返回true
2.3 逻辑运算符
[root@wentan shell]# vim and.sh
#!/bin/bash
a=10
b=20
if [[ $a -lt 100 && $b -gt 100 ]]
then
echo "True"
else
echo "false"
fi
if [[ $a -lt 100 && $b -gt 100 ]]
then
echo "True"
else
echo "false"
fi
[root@wentan shell]# chmod 755 and.sh
[root@wentan shell]# ./and.sh
false
false
2.4 读取用户选择
[root@wentan shell]# vim read.sh
#!/bin/bash
echo -n "yes or no(y/n)"
read choice
echo $choice
[root@wentan shell]# chmod 755 read.sh
[root@wentan shell]# ./read.sh
yes or no(y/n)y
y
[root@wentan shell]# vim readuser.sh
#!/bin/bash
read -p "username: " user
echo $user
[root@wentan shell]# chmod 755 readuser.sh
[root@wentan shell]# ./readuser.sh
[root@wentan shell]# ./readuser.sh
username: wentan
wentan
2.6 一键增加用户
[root@wentan shell]# vim useradd.sh
#!/bin/bash
read -p "username: " user
read -p "password: " pass
useradd "$user"
echo "$pass" | passwd --stdin "$user"
[root@wentan shell]# chmod 755 useradd.sh
[root@wentan shell]# ./useradd.sh
username: tan-1210
password: tan-1210
Changing password for user tan-1210.
passwd: all authentication tokens updated successfully.
2.7 ping同网站的所有主机
[root@wentan shell]# vim ping.sh
#!/bin/bash
for i in {1..254}
do
ping -c 2 -i 0.3 -w 1 192.168.253.$i &> /dev/null
if [ $? -eq 0 ];then
echo "192.168.253.$i is up"
else
echo "192.168.253.$i is down"
fi
done
[root@wentan shell]# chmod 755 ping.sh
[root@wentan shell]# ./ping.sh
192.168.253.1 is up
192.168.253.2 is up
192.168.253.3 is down
...
192.168.253.128 is up
...
2.8 报警
[root@wentan shell]# vim warning.sh
#!/bin/bash
disk_size=$(df / | awk '/\//{print $4}')
mem_size=$(free | awk '/Mem/{print $4}')
while :
do
if [ $disk_size -lt 10240000 -o $mem_size -lt 10240 ]
then
echo "warning! current memory is :$mem_size current disk is : $disk_size"
else
echo "system well"
fi
break
done
[root@wentan shell]# chmod 755 warning.sh
[root@wentan shell]# ./warning.sh
system well
2.9 统计 log 文件内容
[root@wentan shell]# vim log.sh
#!/bin/bash
cd /var/log
sum = 0
for i in `ls -r *`
do
if [ -f $i ];then #检查是否为文件
let sum++
echo "文件名: $i"
fi
done
echo "总文件数量为:$sum"
[root@wentan shell]# chmod 755 log.sh
[root@wentan shell]# ./log.sh
sum: '=': No such file or directory
sum: 0: No such file or directory
文件名: Xorg.9.log
文件名: wtmp
文件名: vmware-vmusr.log
文件名: vmware-vmsvc.log
文件名: vmware-vgauthsvc.log.0
文件名: vmware-network.log
文件名: vmware-network.9.log
文件名: vmware-network.8.log
文件名: vmware-network.7.log
文件名: vmware-network.6.log
文件名: vmware-network.5.log
文件名: vmware-network.4.log
文件名: vmware-network.3.log
文件名: vmware-network.2.log
文件名: vmware-network.1.log
文件名: spooler
文件名: secure
文件名: README
文件名: messagez~
文件名: messages~
文件名: messages
文件名: maillog-20220116
文件名: maillog-20220115
文件名: maillog
文件名: lastlog
文件名: hawkey.log-20220116
文件名: hawkey.log-20220115
文件名: hawkey.log
文件名: firewalld
文件名: dnf.rpm.log-20220116
文件名: dnf.rpm.log-20220115
文件名: dnf.rpm.log
文件名: dnf.log-20220116
文件名: dnf.log-20220115
文件名: dnf.log
文件名: dnf.librepo.log-20220116
文件名: dnf.librepo.log-20220115
文件名: dnf.librepo.log
文件名: cron-20220116
文件名: cron-20220115
文件名: cron
文件名: btmp
文件名: boot.log-20220122
文件名: boot.log-20220120
文件名: boot.log-20220119
文件名: boot.log-20220116
文件名: boot.log-20220115
文件名: boot.log-20220114
文件名: boot.log-20220112
文件名: boot.log
文件名: hawkey.log
文件名: dnf.librepo.log
总文件数量为:52
2.10 查看网卡试试流量
#循环5次
[root@wentan shell]# vim traffic.sh
#!/bin/bash
for((i=1;i<6;i++))
do
echo "当前网卡ens160的实时数据:"
echo "当前上传流量为: " &&ifconfig ens160 | grep "RX packets" | awk '{print $5}'
echo "当前下载流量为: " &&ifconfig ens160 | grep "TX packets" | awk '{print $5}'
sleep 1
done
[root@wentan shell]# chmod 755 traffic.sh
[root@wentan shell]# ./traffic.sh
当前网卡ens160的实时数据:1
当前上传流量为:
2857892
当前下载流量为:
19092371
当前网卡ens160的实时数据:2
当前上传流量为:
2858032
当前下载流量为:
19092907
当前网卡ens160的实时数据:3
当前上传流量为:
2858172
当前下载流量为:
19093443
当前网卡ens160的实时数据:4
当前上传流量为:
2858312
当前下载流量为:
19093979
当前网卡ens160的实时数据:5
当前上传流量为:
2858452
当前下载流量为:
19094515
2.11 检测用户并关机
[root@wentan shell]# vim root.sh
#!/bin/bash
if [ $USER == "root" ]
then
echo "welcome root"
else
poweroff
fi
if [ $UID == 0 ]
then
echo "welcome root"
else
poweroff
fi
[root@wentan shell]# chmod 755 root.sh
[root@wentan shell]# ./root.sh
welcome root
2.12 监控进程
[root@wentan shell]# vim pid.sh
#!/bin/bash
running=0
sleeping=0
stopped=0
zombie=0
for pid in /proc/[1-9]*
do
process=$[process+1]
status=$(awk '{print $3}' $pid/stat)
case $status in
R)
running=$[running+1]
;;
T)
stopped=$[stopped+1]
;;
S)
sleeping=$[sleeping+1]
;;
Z)
zombie=$[zombie+1]
;;
esac
done
echo "进程统计信息如下:"
echo "总进程数为:$process"
echo "Running 进程数为:$running "
echo "Stopped 进程数为:$stopped "
echo "Sleeping 进程数为:$sleeping "
echo "Zombie 进程数为:$zombie "
[root@wentan shell]# chmod 755 pid.sh
[root@wentan shell]# ./pid.sh
进程统计信息如下:
总进程数为:302
Running 进程数为:0
Stopped 进程数为:3
Sleeping 进程数为:199
Zombie 进程数为:0
视频讲解在微信公众号
问渠清源
回复关键词视频
即可观看