Linux 基础入门 11 (End)
视频讲解在微信公众号
问渠清源
回复关键词视频
即可观看
一、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/bashecho "hello,world"[root@wentan dwt]# lltotal 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 $namewentan
-
撤销变量
[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 #输出的是结果13expr 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执行command1elif [a>b 返回值为true]then 执行command2elif [a<b 返回值为true]then 执行command3fi
1.5.2 case 语句
case number innumber>10) command1 ;;number =10) command2 ;;number<10) command3 ;;esac
1.5.3 for 语句
for (( i=1; i<10;i++))do command1doneuntil语句until test command1do command2done
二、shell 编程实战
2.1 数值运算符
[root@wentan ~]# cd /dwt/[root@wentan dwt]# mkdir shell[root@wentan dwt]# cd shell/[root@wentan shell]# vim math.sh#!/bin/basha=10b=20if [ $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/basha=10b=20if [ $a != $b ]then echo "$a != $b : a不等于b"else echo "$a == $b : a等于b"fiif [ $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不等于b10 小于 100 且 20 大于 15 : 返回true
2.3 逻辑运算符
[root@wentan shell]# vim and.sh #!/bin/basha=10b=20if [[ $a -lt 100 && $b -gt 100 ]]then echo "True"else echo "false"fiif [[ $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 falsefalse
2.4 读取用户选择
[root@wentan shell]# vim read.sh#!/bin/bashecho -n "yes or no(y/n)"read choiceecho $choice[root@wentan shell]# chmod 755 read.sh [root@wentan shell]# ./read.sh yes or no(y/n)yy
[root@wentan shell]# vim readuser.sh#!/bin/bashread -p "username: " userecho $user[root@wentan shell]# chmod 755 readuser.sh [root@wentan shell]# ./readuser.sh[root@wentan shell]# ./readuser.sh username: wentanwentan
2.6 一键增加用户
[root@wentan shell]# vim useradd.sh#!/bin/bashread -p "username: " userread -p "password: " passuseradd "$user"echo "$pass" | passwd --stdin "$user"[root@wentan shell]# chmod 755 useradd.sh [root@wentan shell]# ./useradd.shusername: tan-1210password: tan-1210Changing password for user tan-1210.passwd: all authentication tokens updated successfully.
2.7 ping同网站的所有主机
[root@wentan shell]# vim ping.sh#!/bin/bashfor 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" fidone[root@wentan shell]# chmod 755 ping.sh [root@wentan shell]# ./ping.sh 192.168.253.1 is up192.168.253.2 is up192.168.253.3 is down...192.168.253.128 is up...
2.8 报警
[root@wentan shell]# vim warning.sh#!/bin/bashdisk_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 breakdone[root@wentan shell]# chmod 755 warning.sh[root@wentan shell]# ./warning.sh system well
2.9 统计 log 文件内容
[root@wentan shell]# vim log.sh#!/bin/bashcd /var/logsum = 0for i in `ls -r *`do if [ -f $i ];then #检查是否为文件 let sum++ echo "文件名: $i" fidoneecho "总文件数量为:$sum"[root@wentan shell]# chmod 755 log.sh [root@wentan shell]# ./log.sh sum: '=': No such file or directorysum: 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/bashfor((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 1done[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/bashif [ $USER == "root" ]then echo "welcome root"else powerofffiif [ $UID == 0 ]then echo "welcome root"else powerofffi [root@wentan shell]# chmod 755 root.sh [root@wentan shell]# ./root.sh welcome root
2.12 监控进程
[root@wentan shell]# vim pid.sh#!/bin/bashrunning=0sleeping=0stopped=0zombie=0for 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] ;; esacdoneecho "进程统计信息如下:"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 进程统计信息如下:总进程数为:302Running 进程数为:0 Stopped 进程数为:3 Sleeping 进程数为:199 Zombie 进程数为:0
视频讲解在微信公众号
问渠清源
回复关键词视频
即可观看