介绍
白树明
敲代码的流川枫
天易IT学院
北京天易博通科技有限公司 执行总裁
12小时视频
https://www.bilibili/video/BV1j541157Sr?p=1
流老师的笔记地址
https://book.apeland/details/274/
本示例在 zhan 192.168.121.37 上面执行
P01_为什么学习shell 22:52
P02_shell介绍 12:04
P03_理解shell脚本精髓 15:46
P04_shell脚本语法规范1 29:21
P05_shell语法2 32:13
P06_shell格式化输出 25:28
P07_shell程序交互 19:30
P08_变量介绍 15:17
P09_shell变量类型 08:50
P10_shell变量管理 13:31
P11_shell数组 23:37
P12_shell数据比较运算 16:49
P13_文件类型判断 15:03
P14_其他运算 09:42
P15_if语法 25:34
P16_for语法 26:19
P17_shell循环控制 31:33
P18_while语法 22:58
P19_while循环控制与语句嵌套 19:14
P20_until循环 07:15
P21_case语句 19:37
P22_函数介绍 16:17
P23_nginx_启动管理脚本实战 33:46
P24_正则表达式之特殊字符 21:00
P25_正则表达式POSIX字符 16:24
P26_shell对文件的操作 24:29
P27_sed命令详解 18:01
P28_shell_awk基本语法 28:39
P29_awk高级用法1 24:00
P30_awk高级语法2 19:27
P31_监测一个主机状态 23:26
P32_监控一个服务端口 20:57
P33_监控memory使用率 14:17
P34_统计使用内存或CPU前十名进程 25:10
P35_io_队列长度监控 22:34
P36_监控脚本总结 14:45
P36_nginx_install_1 25:26
P38_nginx_install_02 23:30
P39_lamp安装脚本 16:23
P40_mysql_备份脚本 32:06
P41_mysql_备份脚本排错方法 18:30
P42_创建user01-user20用户 23:53
P01_为什么学习shell 22:52
shell perl python 三种脚本
shell 学习分为几个阶段
- 能看懂shell脚本
2)能改shell脚本
3)能自己写shell脚本
4)能优化shell脚本
P02_shell介绍 12:04
shell是一个程序,采用C语言编写,是用户和linux内核沟通的桥梁。
kernel: 管理硬件,驱动硬件做事
shell: 命令解释器
user: 用户接口对接用户
[root@aliyunleo ~]# echo "hello world"
hello world
[root@aliyunleo ~]# echo "hello world" | sed 's/world/dddd/'
hello dddd
[root@aliyunleo ~]# echo $USER
root
P03_理解shell脚本精髓 15:46
如何书写一个shell
shell脚本运行
shell中的特殊符号
管道
重定向
shell中数学运算
脚本退出
shell脚本就是将完成一个任务的所有命令按照执行的先后顺序,自伤而下写入到一个文本文件中,然后给予执行权限。
第一个shell脚本
第一个shell脚本编写过程,自动安装nginx
step1 进入 相应目录 新建nginx_install.sh 文件
/opt/shell/shell_03
touch nginx_install.sh
step2 编辑文件
yum install -y wget gcc pcre-devel zlib-devel
wget http://nginx/download/nginx-1.16.0.tar.gz
tar xf nginx-1.16.0.tar.gz
cd nginx-1.16.0
./configure --prefix=/usr/local/nginx
make -j 1
make install
step3
chmod 700 nginx_install.sh
step4 执行脚本
./nginx_install.sh
step5
--启动nginx
/usr/local/nginx 会有相应的文件
进入 /usr/local/nginx/sbin
执行 ./nginx 启动 nginx
step6 关闭防火墙
sysctmctl stop firewalld
systemctl disable firewalld
step7 测试
到实体机上面访问相应网站,本示例为 192.168.121.37
http://192.168.121.37
P04_shell脚本语法规范1 29:21
shell 语法有:
- 如何书写一个脚本
- shell 脚本运行
- shell 中的特殊符号
- 管道
- 重定向
- shell 中数学运算
- 脚本退出
1 如何定义一个脚本
- #定义脚本的执行环境 #! 是组合,不是注释
[root@shell37 sbin]# which bash
/usr/bin/bash
#!/usr/bin/bash
2.# 是注释
- 脚本信息
#Author: weilei
#Created Time: 2021/03/03 16:33:33
#Release: 2.2
#Script Description: nginx install script
编辑VIM可以自动出这个
- 脚本组成
#解释环境
#注释说明
#执行代码
2 如何运行一个脚本
touch a1.sh
echo “hello world”
- 给执行权限 chmod 700 a1.sh 然后执行 ./a.sh
- 使用解释器直接执行 不需要给予权限 bash a1.sh | sh a1.sh
- 查看当前环境的解释器 cat /etc/shells
[root@shell37 shell_04]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
3 shell 中的特殊符号
符号 | 解释 |
---|---|
~ | 家目录 cd ~ 代表进入用户家目录 |
! | 执行历史命令 !! 执行上一条命令 |
$ | 变量中取内容符 |
±*/% | 数学运算 |
& | 后台执行 |
* | 通配符 匹配所有 |
? | 通配符 匹配除回车外 任意一个字符 |
; | 多个命令之间用; |
| | 管道符 ,上一个命令输出作为下一个命令输入 |
\ | 转移字符 |
`` | 反引号 命令中执行命令 echo “ today is date +%F ” 输出 today is : 2021-03-03 |
‘’ | 单引号不解释变量 echo ‘$USER’ 输出 $USER |
“” | 脚本中的字符串 echo “$USER” 输出 root |
P05_shell语法2 32:13
shell 语法有:
- 如何书写一个脚本
- shell 脚本运行
- shell 中的特殊符号
- 管道
- 重定向
- shell 中数学运算
- 脚本退出
5.重定向
> 重定向输入
>> 重定向追加输入
< 重定向输出 wc -l < /etc/passwd
<< 重定向追加输出 fdisk /dev/sdb <<EFO … EOF
划分磁盘空间指令
对fdisk 指令需要单独学习,暂时没有懂 20210304
----查看所有磁盘
[root@shell37 ~]# fdisk -l
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: 0x000b815d
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 83 Linux
/dev/sda2 616448 4810751 2097152 82 Linux swap / Solaris
/dev/sda3 4810752 41943039 18566144 83 Linux
----显示 /dev/sda3 是否有分区
[root@shell37 ~]# fdisk -l /dev/sda3
Disk /dev/sda3: 19.0 GB, 19011731456 bytes, 37132288 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_partition.sh
#!/usr/bin/bash
#Author:wl
#Create Time : 20210304
#Script description: disk partition script
fdisk /dev/sda3 <<EOF
n
p
4
+100M
w
EOF
执行 bash disk_partition.sh 命令后 ,看到 /dev/sda3 已经分配了 /dev/sda3p4 磁盘空间
[root@shell37 ~]# fdisk -l /dev/sda3
Disk /dev/sda3: 19.0 GB, 19011731456 bytes, 37132288 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: 0x1173942e
Device Boot Start End Blocks Id System
/dev/sda3p4 1093632 1298431 102400 83 Linux
6. shell 中数学运算
expr
[root@shell37 shell_04]# expr 1 + 1
2
[root@shell37 shell_04]# expr 1 - 1
0
[root@shell37 shell_04]# expr 1 \* 2
2
[root@shell37 shell_04]# expr 10 / 4
2
[root@shell37 shell_04]# expr 1 + 2.2.
expr: non-integer argument
[root@shell37 shell_04]# expr 1 + 0
1
[root@shell37 shell_04]# echo $? --- 打印上一条结果
0
[root@shell37 shell_04]# expr 1 + 2.2
expr: non-integer argument
[root@shell37 shell_04]# echo $?
2
[root@shell37 shell_04]# expr 1 + 7 &> /dev/null ; echo $?
0
let
[root@shell37 shell_04]# let a=1+2
[root@shell37 shell_04]# echo $a
3
bc
[root@shell37 shell_04]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+1
2
3*3
9
5/4
1
(1+2)*3/2+444
448
scale =2 -------设置2位小数点
4/3
1.33
[root@shell37 ~]# echo "scale=2; 142*1000/2333" | bc;
60.86
[root@shell37 ~]# echo "`echo "scale=2; 142*1000/2333" | bc;` %"
[root@shell37 ~]# echo "当 前 内存 使用 率 : `echo "scale=2; 142*1000/2333" | bc;` %"
当前 内存 使用 率 : 60.86 %
$(()) 做运算
# 其中的数据只能是整数
[root@shell37 shell]# echo $((18*10))
180
7. 脚本退出
exit
vim exit.sh
#!/bin/bash
echo "haha"
exit 2
[root@shell37 shell]# bash exit.sh
haha
[root@shell37 shell]# echo $?
2
P06_shell格式化输出 25:28
-n — 后面不换行
-e — 后面解释为转移字符
\t —tab
\b – 删除 前一个字符
\n —换行且光标移至行首
倒计时 time.bash
版本0.1
vim time.sh
#!/bin/bash
for time in `seq 9 -1 0`;do
echo $time
done
~
[root@shell37 shell]# sh time.sh
9
8
7
6
5
4
3
2
1
版本1.0
echo -n -e "\b$time" --- -n 不换行 -e 解释后面的
echo ---最后换行
#!/bin/bash
for time in `seq 9 -1 0`;do
echo -n -e "\b$time"
sleep 1
done
echo
~
~
水果商店
#!/bin/bash
echo -e "\t\t\t\t\t Fruits Shop"
echo -e "\t1) Apple"
echo -e "\t2) Orange"
echo -e "\t3) Banana"
[root@shell37 shell]# sh fruits.sh
Fruits Shop
1) Apple
2) Orange
3) Banana
颜色代码
echo -e "\033[背景色; 文字色m 字符串 \033[特效m
echo -e “\033[41;36m someting here \033[0m”
背景色范围: 40–47
文字颜色范围: 30-37
特效: 0-8
echo -e "\033[41;31m someting here \033[4m"
本机示例没有效果 ,愿意可能是自己本机的vim的原因???
P07_shell程序交互 19:30
第七节 程序交互
read 命令选项
-p 打印信息
-t 限定时间
-s 不回显
-n 输入字符个数
#!/bin/bash
clear
echo -n -e "Login:"
read
echo -n -e "Password:"
read
echo "account: password: "
[root@shell37 shell]# sh read_command.sh
Login:ss
Password:pwd
account: password:
[root@shell37 shell]#
vim read_command2.sh
#!/bin/bash
clear
echo -n -e "Login:"
read name
echo -n -e "Password:"
read pwd
echo "account: $name password: $pwd "
[root@shell37 shell]# sh read_command2.sh
Login:ss
Password:pwd
account: ss password: pwd
[root@shell37 shell]#
#!/bin/bash
clear
echo -n -e "Login:"
read name
echo -n -e "Password:"
#-s security do not show text
#-t9 9 seconds exit
#-n6 max length is 6
read -s -t9 -n6 pwd
echo
echo "account: $name password: $pwd "
#!/bin/bash
clear
read -p "Login:" name
echo -n -e "Password:"
read pwd
echo
echo "account: $name password: $pwd "
~
Password:sss
account: wel password: sss
[root@shell37 shell]#
[root@shell37 shell]# vim mockLogin.sh
#!/usr/bin/bash
echo "CentOS Linux 7 (Core)"
echo "Kernel `uname -r ` an `uname -m` \n "
echo -n -e "$HOSTNAME login: "
read acc
read -p "password: "
read pwd
[root@shell37 shell]# sh mockLogin.sh
CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 an x86_64 \n
shell37 login: wl
password: ll
[root@shell37 shell]#
P08_变量介绍 15:17
变量介绍
变量分类
变量管理
P09_shell变量类型 08:50
变量分类:
1.本地变量:用户私有变量,只有本用户使用放在加目录 .bash_profile/ .bashrc文件中
2.全局变量:所有用户都可以使用, 保存在 /etc/profile / etc/bashrc 文件中
3.用户自定义变量:脚本中用户自定义的变量
NAME='LILI'
AGE=22
SCORE=100
echo "name:$NAME,age:$AGE,score:$SCORE"
~
[root@shell37 shell]# sh vartest.sh
name:LILI,age:22,score:100
P10_shell变量管理 13:31
1.变量定义 XX=123;
2.变量读取 echo $XX;
3.取消变量 unset XX
注意 变量名与等号之间没有空格
老师建议变量名为大写
[root@shell37 shell]# NAME=LILI ----- OK
[root@shell37 shell]# NAME2 = LEILEI ----- FAILED
bash: NAME2: command not found...
[root@shell37 shell]# XX=33
[root@shell37 shell]# echo $XX;
33
[root@shell37 shell]# unset XX;
[root@shell37 shell]# echo $XX
[root@shell37 shell]# vim /etc/profile
GENDER='male' ---- 本地变量
export SEX='man' ----全局变量
[root@shell37 shell]# echo $GENDER
[root@shell37 shell]# echo $SEX
[root@shell37 shell]# source /etc/profile
[root@shell37 shell]# echo $GENDER
male
[root@shell37 shell]# echo $SEX
man
P11_shell数组 23:37
数组介绍
基本数组
关联数组
案例分享
基本数组
[root@shell37 shell]# vim arr.sh
#!/bin/bash
#test array
#01 批量声明数组 及 打印某个数组项
username=(weilei xiaosan lisi zhangsan)
echo ${username[2]}
#02 单独声明某个数组项
ARRAY2=('1' '2')
ARRAY2[2]='c'
echo ${ARRAY2[2]}
# 查看所有数组
declare -a
# ARRAY2=1 2 c D
# single c
echo ${ARRAY2[2]}
echo '0901....'
# 打印所有数组 1 2 c D
echo ${ARRAY2[@]}
echo ${ARRAY2[*]}
# 统计数组元素个数 4
echo ${#ARRAY2[@]}
echo '0902....'
# 打印所有数组索引 0 1 2 3
echo ${!ARRAY2[@]}
echo '0903....'
# 从下标1开始打印 2 c D
# 从下标1开始打印 打印两个 2 c
echo ${ARRAY2[@]:1}
echo ${ARRAY2[@]:1:2}
echo '0904....'
关联数组
# 声明为关联数组
declare -A ass_array1
declare -A ass_array2
# 一个一个赋值
ass_array1[name]='weilei'
ass_array1[age]=18
echo ${ass_array1[name]}
echo ${ass_array1[age]}
echo '......'
# 批量赋值
ass_array2=([name1]=tom [name2]=jim [name3]=lili [name4]=cherry)
echo ${ass_array2[name2]}
echo '.......'
[root@shell37 shell]# sh arr2.sh
weilei
18
......
jim
截图
P12_shell数据比较运算 16:49
shell 中的五大运算符
1)数学比较运算符 (-eq 等于 -gt 大于 -lt 小于 -ge 大于或等于 -le 小于或等于 -ne 不等于 )
2)字符串比较运算 ( )
3)
4)
5)
float 的处理方法
#$(()) 只支持整数运算
[root@shell37 shell]# echo $((18*10))
180
# 计算 1.5 * 10
[root@shell37 shell]# echo "1.5*10"|bc
15.0
# 将 15.0 按照 . 分割为两部分 打印第一部分
[root@shell37 shell]# echo "1.5*10"|bc|cut -d '.' -f1
15
[root@shell37 shell]# cat floattest.sh
#!/bin/bash
# 判断 1.5 与 2 是否相等 ,由于无法支持浮点数 ,所以要 * 10 放到临时变量后,再比较
NUM1=`echo "1.5*10"|bc|cut -d "." -f1 `
NUM2=$((2*10))
test $NUM1 -eq $NUM2;echo $?
# 直接打印结果
[root@shell37 shell]# sh floattest.sh
1
# -x 参数 打印整个DEBUG的 过程
[root@shell37 shell]# sh -x floattest.sh
++ echo '1.5*10'
++ bc
++ cut -d . -f1
+ NUM1=15
+ NUM2=20
+ test 15 -eq 20
+ echo 1
1
P13_文件类型判断 15:03
-d 判断目录 document
-f 判断文件 file
-e 判断目录及文件 either
[root@shell37 shell]# test -d /emp/abc; echo $?
1
[root@shell37 shell]# test -d /tmp/abc; echo $?
1
[root@shell37 shell]# mkdir /tmp/abc
[root@shell37 shell]# test -d /tmp/abc; echo $?
0
[root@shell37 shell]# test -f /tmp/abc; echo $?
1
[root@shell37 shell]# test -e /tmp/abc; echo $?
0
--- test -s 检查文件是否存在且不为空
[root@shell37 shell]# test -s /opt/shell/file0306.txt ; echo $?
1
[root@shell37 shell]# ll /opt/shell/file0306.txt
-rw-r--r--. 1 root root 0 Mar 5 19:23 /opt/shell/file0306.txt
---- test -r 检查文件是否存在且可读
---- test -w 检查文件是否存在且可写
---- test -x 检查文件是否存在且可执行
---- test -O 检查文件是否存在且被当前用户拥有
[root@shell37 shell]# test -r /opt/shell/file0306.txt ; echo $?
0
[root@shell37 shell]# test -w /opt/shell/file0306.txt ; echo $?
0
[root@shell37 shell]# test -x /opt/shell/file0306.txt ; echo $?
1
[root@shell37 shell]# test -s /opt/shell/file0306.txt ; echo $?
1
[root@shell37 shell]# echo > file0306.txt
[root@shell37 shell]# test -s /opt/shell/file0306.txt ; echo $?
0
[root@shell37 shell]# test -O /opt/shell/file0306.txt ; echo $?
0
[root@shell37 shell]# test -G /opt/shell/file0306.txt ; echo $?
0
---- test file1 -nt file2 ---文件1是否比文件2 新 new than
---- test file1 -ot file2 ---文件1是否比文件2 旧 than
[root@shell37 shell]# test file0306.txt -nt floattest.sh ; echo $?
0
[root@shell37 shell]# test file0306.txt -ot floattest.sh ; echo $?
1
P14_其他运算 09:42
== 等于
!= 不等于
-n 检查字符串长度是否大于0
-z 检查字符串长途是否等于0
[root@shell37 shell]# echo $USER
root
[root@shell37 shell]# test $USER == "root"; echo $?;
0
[root@shell37 shell]# test $USER == "root1"; echo $?;
1
[root@shell37 shell]# test "root2" != "root1" ;echo $?
0
[root@shell37 shell]# test -n "root2"; echo $?
0
[root@shell37 shell]# test -z "root3"; echo $?
1
---- 提前学习if
[root@shell37 shell]# if [ 1 -eq 1 ] && [ 2 -eq 2 ] ; then echo "yes"; else echo "no" ; fi
yes
[root@shell37 shell]# if [ 1 -eq 1 ] && [ 2 -eq 3 ] ; then echo "yes"; else echo "no" ; fi
no
[root@shell37 shell]#
P15_if语法 25:34
单If 语法
vim iftest.sh
#!/bin/bash
# if not exit /tmp/abc then create
# 如果不存在目录 /tmp/abc ...
if [ ! -d /tmp/abc ]
then
mkdir -v /tmp/abc
echo "123"
echo "create /tmp/abc is ok "
fi
[root@shell37 shell]# sh iftest.sh
mkdir: created directory ‘/tmp/abc’
123
create /tmp/abc is ok
if-then-else 语句
# 如果是root 显示 hi admin ; 如果是其他用户显示 hi guest
if [ $USER == 'root' ]
then
echo "hi admin"
else
echo "hi guest "
fi
[root@shell37 shell]# sh iftest02.sh
hi admin
-----切换到 leo 用户下面
[root@shell37 shell]# su leo
[leo@shell37 shell]$ sh iftest02.sh
hi guest
------ 使用 leo 用户执行 bash **** 指令
[root@shell37 shell]# su - leo -c "bash /opt/shell/iftest02.sh"
hi guest
注意语法结构 if [ ] 中括号中的内容是要有空格的
if [$1 -eq $2] ----语法错误
if [ $1 -eq $2 ] ----正确写法
[root@shell37 shell]# vim iftest03.sh
#!/bin/bash
if [ $1 -eq $2 ]
then
echo "$1 = $2"
else
if [ $1 -gt $2 ]
then
echo "$1 > $2"
else
echo "$1 < $2"
fi
fi
[root@shell37 shell]# bash iftest03.sh 2 3
2 < 3
[root@shell37 shell]# bash iftest03.sh 2 2
2 = 2
[root@shell37 shell]# bash iftest03.sh 2 1
2 > 1
[root@shell37 shell]# vim iftest04.sh
if [ $1 -gt $2 ]
then
echo "$1>$2"
elif [ $1 -eq $2 ]
then
echo "$1=$2"
else
echo "$1<$2"
fi
# 判断如果 ¥i 以r开头 就打印 否则 不打印
echo "=============================="
for i in r1 rr2 cc rr3
do
if [[ $i == r* ]]; then
echo $i
fi
done
==============================
r1
rr2
rr3
P16_for语法 26:19
for循环介绍
for 语法
- for i in 写法
- C式 for 命令 写法
for 循环控制
[root@shell37 shell]# vim fortest1.sh
#!/bin/bash
echo "=================================="
for i in 1 2 3 4 7
do
echo $i
done
==================================
1
2
3
4
7
echo "--------------"
for i in `seq 1 9`
do
echo $i
done
--------------
1
2
3
4
5
6
7
8
9
echo "=================================="
for((i=1;i<10;i++))
do
echo $i
done
==================================
1
2
3
4
5
6
7
8
9
echo "=================================="
for(( n=10,m=0; n>0,m<10;n--,m++ ))
do
echo -e "$n\t$m"
done
==================================
10 0
9 1
8 2
7 3
6 4
5 5
4 6
3 7
2 8
1 9
P17_shell循环控制 31:33
- sleep n 休眠 N 秒
- continue 跳过循环中的某次循环
- break 跳出循环继续执行后续代码
sleep
验证 某台主机是否存活
---- ping -c1 $1 ping 一台 主机 一次
[root@shell37 shell]# vim pcisalive.sh
for (( ;; ))
do
ping -c1 $1 &>/dev/null
if [ $? -eq 0 ]
then
echo "`date +"%F %H:%M:%S"` :$1 is UP "
else
echo "`date +"%F %H:%M:%S"` :$1 is down "
fi
sleep 5
done
[root@shell37 shell]# bash pcisalive.sh 192.168.121.38
2021-03-06 00:08:03 :192.168.121.38 is UP
2021-03-06 00:08:08 :192.168.121.38 is UP
2021-03-06 00:08:13 :192.168.121.38 is UP
优化上个写法,增加颜色
[root@shell37 shell]# vim pcisalive2.sh
for (( ;; ))
do
ping -c1 $1 &>/dev/null
if [ $? -eq 0 ]
then
echo -e "`date +"%F %H:%M:%S"` :$1 is \033[32m UP \033[0m "
else
echo -e "`date +"%F %H:%M:%S"` :$1 is \033[31m down \033[0m "
fi
sleep 5
done
continue
[root@shell37 shell]# vim continuetest.sh
for ((i=1;i<10;i++))
do
if [ $i -eq 5 ] ; then
# 本次循环结束 ,可以进行下一次循环了
continue
fi
echo $i
done
[root@shell37 shell]# bash continuetest.sh
1
2
3
4
6
7
8
9
break
[root@shell37 shell]# vim breaktest2.sh
for ((i=1; i<100; i++ ))
do
echo "#loop $i"
for (( ;; ))
do
echo "leo"
# break return current ; break 2 return 2 level
break 2
done
sleep 4
done
P18_while语法 22:58
while 循环介绍
while 循环语法
while 实战
while的条件是五种运算类型
- 数学比较
- 字符串比较
- 文件类型
- 逻辑运算
- 赋值运算
read -p "num:" num1
while [ $num1 -gt 0 ]
do
echo "big than zero"
sleep 3
done
-----输出 root 后 退出循环 ,否则 一直输入
[root@shell37 shell]# vim while_02.sh
read -p "login:" account
while [ $account != "root" ]
do
read -p "login:" account
done
[root@shell37 shell]# vim while_03.sh
while [ ! -d /tmp/weilei ]
do
echo “not found /tmp/weilei”
sleep 3
done
丈母娘选女婿问题
钱大于 10万 ,有 车 ,有 房 ,可以将我女儿带走
[root@shell37 tmp]# vim while_boyfriend.sh
read -p "money:" money
read -p "car:" car_num
read -p "house:" house_num
while [ $money -lt 10000 ] || [ $car_num -lt 1 ] || [ $house_num -lt 1 ]
do
echo "not allowed "
read -p "money:" money
read -p "car:" car_num
read -p "house:" house_num
done
echo "it's yours "
[root@shell37 shell]# bash while_boyfriend.sh
money:2
car:2
house:2
not allowed
money:19999999
car:2
house:2
it's yours
read -p "char:" ch
while [ $ch != "Q" ]
do
read -p "char:" ch
done
[root@shell37 shell]# bash while_quit.sh
char:3 --- 输入三继续
char:Q ---- 输入q 退出
[root@shell37 shell]#
P19_while循环控制与语句嵌套 19:14
- 嵌套 if for while
- 循环控制 sleep break continue
while [ $i -lt 10 ]
do
echo $i
i=$((i+1))
done
[root@shell37 shell]# vim while_other.sh
i=1
while [ $i -lt 10 ]
do
echo $i
if [ $i -eq 5 ]; then
break
fi
i=$((i+1))
done
乘法口诀01
echo "---------------------乘法口诀表 wtc 定制版 -------------------------------"
echo " 4 * 7 = 28 | 4 * 7 = 28 "
echo
echo
echo
# read -p "input num: " num
for (( i=1 ; i<= 9; i++))
do
for (( j=1 ;j<=i; j++ ))
do
echo -e -n "$j * $i = $(( $j * $i )) "
done
echo
done
echo "=====================乘法口诀 老师的版本 ====================="
n=1
while [ $n -lt 10 ] ;do
for (( m=1;m<=$n;m++ ));do
echo -n -e " $m * $n = $((n*m)) \t"
done
echo
n=$((n+1))
done
==========================================
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
[root@shell37 shell]#
P20_until循环 07:15
与 while 恰好相反
num=10
until [ $num -gt 20 ]; do
echo "current val is $num "
num=$(( num+1 ))
done
[root@shell37 shell]# bash untiltest.sh
current val is 10
current val is 11
current val is 12
current val is 13
current val is 14
current val is 15
current val is 16
current val is 17
current val is 18
current val is 19
current val is 20
关于 空格
num=1
while [ $num -lt 10 ]; do
echo $num
num=$((num+1))
until [ $num -lt 10 ]; do
echo $num
if [ $num -eq 20 ]; then
break
fi
num=$((num+1))
done
done
[root@shell37 shell]# bash untiltest.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
P21_case语句 19:37
[root@shell37 shell]# vim casetest.sh
#!/bin/bash
read -p "NUM:" N
case $N in
1)
echo "haha01"
;;
2)
echo "22222"
;;
*)
echo "others"
;;
esac
[root@shell37 shell]# bash casetest.sh
NUM:1
haha01
[root@shell37 shell]# bash casetest.sh
NUM:22
others
女婿进入丈母娘家示例
[root@shell37 shell]# vim first2wifehome.sh
#!/bin/bash
case $1 in
zmn|ZMN)
echo "ni hao mama"
;;
lzr|LZR)
echo "ni hao dada"
;;
lnn|LNN)
echo "ni hao nainai"
;;
*)
echo "usage: $0 zmn|lzr|lnn"
;;
esac
[root@shell37 shell]# bash first2wifehome.sh
usage: first2wifehome.sh zmn|lzr|lnn
[root@shell37 shell]# bash first2wifehome.sh zmn
ni hao mama
[root@shell37 shell]# bash first2wifehome.sh lzr
ni hao dada
[root@shell37 shell]# bash first2wifehome.sh lzn
usage: first2wifehome.sh zmn|lzr|lnn
echo "script name is: $0 "
echo "script param is $*"
echo "script cnt is $#"
echo "script process id is $$"
echo "last cmd is $_"
echo "2nd param is $2"
[root@shell37 shell]# bash shell_var.sh 11 22 33 44 55
script name is: shell_var.sh
script param is 11 22 33 44 55
script cnt is 5
script process id is 22631
last cmd is script process id is 22631
2nd param is 22
P22_函数介绍 16:17
#!/bin/bash
# function define type 1
start () {
echo "apache start ..... [OK]"
#return 0
}
stop () {
echo "apache stop ..... [ok]"
}
#function define type 2
function running (){
echo "apache is running ......"
}
#invoke function
start
running
stop
[root@shell37 shell]# bash functiontest.sh
apache start ..... [OK]
apache is running ......
apache stop ..... [ok]
P23_nginx_启动管理脚本实战 33:46
太复杂了,看不懂
20210306
P24_正则表达式之特殊字符 21:00
定位符
以a开头 c 结尾 . 匹配除回车外的任意字符
[root@shell37 shell]# grep "^a.c$" testfile.txt
acc
abc
a_c
aZc
a c
a3c
a*c
a*c
匹配符
匹配以a或者b 开头 中间任意字符 c 结尾
[root@shell37 shell]# egrep "^(a|b).c$" testfile.txt
acc
abc
a_c
aZc
a c
a3c
a*c
a*c
bbc
匹配 以 a 开头 中间是0-9的数字 c 结尾
[root@shell37 shell]# egrep "^a[0-9]c$" testfile.txt
a3c
匹配 以 a 开头 中间不是0-9的数字 c 结尾
[root@shell37 shell]# egrep "^a[^0-9]c$" testfile.txt
acc
abc
a_c
aZc
a c
a*c
a*c
匹配 以 a 开头 中间是字符* c 结尾
[root@shell37 shell]# egrep "^a\*c$" testfile.txt
a*c
a*c
限定符
项目 | Value |
---|---|
* | >=0 |
? | 0 或 1 |
+ | >=1 |
{n,m} | n<= x <=m |
{m} | x = m |
---匹配以a开头 ; b不出现 或者出现1次 或 多次 ; c结尾
[root@shell37 shell]# egrep "^ab*c$" testfile.txt
ac
abbc
abbbc
abbbbbc
abc
ac
---匹配以a开头 ; b不出现 或者出现1次 ; c结尾
[root@shell37 shell]# egrep "^ab?c$" testfile.txt
ac
abc
ac
---匹配以a开头 ; b出现1次或者多次 ; c结尾
[root@shell37 shell]# egrep "^ab+c$" testfile.txt
abbc
abbbc
abbbbbc
abc
---匹配以a开头 ; b出现3次 ; c结尾
[root@shell37 shell]# egrep "^ab{3}c$" testfile.txt
abbbc
---匹配以a开头 ; b出现3-5次 ; c结尾
[root@shell37 shell]# egrep "^ab{3,5}c$" testfile.txt
abbbc
abbbbbc
P25_正则表达式POSIX字符 16:24
特殊字符
POSIX特殊字符
特殊字符 | 说明 |
---|---|
[:alnum:] | 匹配任意字母字符0-9 a-z A-Z |
[:alpha:] | 匹配任意字母,大写或小写 |
[:digit:] | 数字 0-9 |
[:graph:] | 非空字符( 非空格控制字符) |
[:lower:] | 小写字符a-z |
[:upper:] | 大写字符A-Z |
[:cntrl:] | 控制字符 |
[:print:] | 非空字符( 包括空格) |
[:punct:] | 标点符号 |
[:blank:] | 空格和TAB字符 |
[:xdigit:] | 16 进制数字 |
[:space:] | 所有空白字符( 新行、空格、制表符) |
[root@shell37 shell]# egrep "^a[[:alnum:]]c$" testfile.txt
acc
abc
aZc
a3c
aac
P26_shell对文件的操作 24:29
perl, sed 对文件进行操作而不进行交互
[root@shell37 shell]# vim seddata.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
----- a append 所有增加 ....thie is appending ...
[root@shell37 shell]# sed 'a\....thie is appending ...' seddata.txt
1 the quick brown fox jumps over the lazy dog.
....thie is appending ...
2 the quick brown fox jumps over the lazy dog.
....thie is appending ...
3 the quick brown fox jumps over the lazy dog.
....thie is appending ...
4 the quick brown fox jumps over the lazy dog.
....thie is appending ...
5 the quick brown fox jumps over the lazy dog.
....thie is appending ...
-----2行 增加 ....thie is appending ...
[root@shell37 shell]# sed '2a\....thie is appending ...' seddata.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
....thie is appending ...
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
-----2,3行增加 ....thie is appending ...
[root@shell37 shell]# sed '2,3a\....thie is appending ...' seddata.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
....thie is appending ...
3 the quick brown fox jumps over the lazy dog.
....thie is appending ...
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
----匹配 3 the ; 在其后面增加 ----appending2---
[root@shell37 shell]# sed '/3 the/a\----appending2---' seddata.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
----appending2---
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
---- i insert 在所有行前面插入append3----
[root@shell37 shell]# sed 'i\append3---' seddata.txt
append3---
1 the quick brown fox jumps over the lazy dog.
append3---
2 the quick brown fox jumps over the lazy dog.
append3---
3 the quick brown fox jumps over the lazy dog.
append3---
4 the quick brown fox jumps over the lazy dog.
append3---
5 the quick brown fox jumps over the lazy dog.
append3---
--- 匹配模式, 在 3 the 前 插入 append3---
[root@shell37 shell]# sed '/3 the/i\append3---' seddata.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
append3---
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
---- d delete 删除
[root@shell37 shell]# sed 'd' seddata.txt
[root@shell37 shell]# sed '3d' seddata.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@shell37 shell]# sed '2,4d' seddata.txt
1 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
---- 删除 以#开头的行
[root@shell37 shell]# sed -r '/^#/d' nginx.conf
------ 删除 (#开头 | 包含# | 空行 )
[root@shell37 shell]# sed -r '/(^#|#|^#)/d' nginx.conf
---替换 dog --> cat....
[root@shell37 shell]# sed 's/dog/cat.../' seddata.txt
1 the quick brown fox jumps over the lazy cat....
2 the quick brown fox jumps over the lazy cat....
3 the quick brown fox jumps over the lazy cat....
4 the quick brown fox jumps over the lazy cat....
5 the quick brown fox jumps over the lazy cat....
P27_sed命令详解 18:01
[root@shell37 shell]# vim seddata01.txt
1 the quick brown fox jumps over the lazy dog.dog
2 the quick brown fox jumps over the lazy dog.dog
3 the quick brown fox jumps over the lazy dog.dog
4 the quick brown fox jumps over the lazy dog.dog
5 the quick brown fox jumps over the lazy dog.dog
---- 替换dog 为 cat 发现只替换了第一个
[root@shell37 shell]# sed 's/dog/cat/' seddata01.txt
1 the quick brown fox jumps over the lazy cat.dog
2 the quick brown fox jumps over the lazy cat.dog
3 the quick brown fox jumps over the lazy cat.dog
4 the quick brown fox jumps over the lazy cat.dog
5 the quick brown fox jumps over the lazy cat.dog
------ 替换了第二处 也就是说 s/dog/cat/ == s/dog/cat/1
[root@shell37 shell]# sed 's/dog/cat/2' seddata01.txt
1 the quick brown fox jumps over the lazy dog.cat
2 the quick brown fox jumps over the lazy dog.cat
3 the quick brown fox jumps over the lazy dog.cat
4 the quick brown fox jumps over the lazy dog.cat
5 the quick brown fox jumps over the lazy dog.cat
--- 替换所有 dog --> cat
[root@shell37 shell]# sed 's/dog/cat/g' seddata01.txt
1 the quick brown fox jumps over the lazy cat.cat
2 the quick brown fox jumps over the lazy cat.cat
3 the quick brown fox jumps over the lazy cat.cat
4 the quick brown fox jumps over the lazy cat.cat
5 the quick brown fox jumps over the lazy cat.cat
sed 命令,命令选项
-n 抑制内存输出 只打印改变行
[root@shell37 shell]# sed -n '3,4s/dog/cat/p' seddata01.txt
3 the quick brown fox jumps over the lazy cat.dog
4 the quick brown fox jumps over the lazy cat.dog
-e 多条件
[root@shell37 shell]# sed -e '3s/brown/green/;3s/dog/cat/' seddata01.txt
1 the quick brown fox jumps over the lazy dog.dog
2 the quick brown fox jumps over the lazy dog.dog
3 the quick green fox jumps over the lazy cat.dog
4 the quick brown fox jumps over the lazy dog.dog
5 the quick brown fox jumps over the lazy dog.dog
-----将要操作的sed指令放到文本中,如下所示,
[root@shell37 shell]# cat sedcmd.txt
s/brown/green/
s/dog/cat/
----- sed -f a b ; a是要执行的指令
[root@shell37 shell]# sed -f sedcmd.txt seddata01.txt
1 the quick green fox jumps over the lazy cat.dog
2 the quick green fox jumps over the lazy cat.dog
3 the quick green fox jumps over the lazy cat.dog
4 the quick green fox jumps over the lazy cat.dog
5 the quick green fox jumps over the lazy cat.dog
---- sed -i 是真正执行修改
---- sed -i.bak 是真正执行修改,且将原文件 放到***.bak 文件下
[root@shell37 shell]# sed -i.bak '3s/brown/green/' seddata01.txt
[root@shell37 shell]# cat seddata01.txt
1 the quick brown fox jumps over the lazy dog.dog
2 the quick brown fox jumps over the lazy dog.dog
3 the quick green fox jumps over the lazy dog.dog
4 the quick brown fox jumps over the lazy dog.dog
5 the quick brown fox jumps over the lazy dog.dog
小技巧
--- 统计文件有多少行
[root@shell37 shell]# sed -n '$=' seddata01.txt
7
P28_shell_awk基本语法 28:39
awk 对输出流处理
awk 是一个行编辑器,完成数据 截取,处理,输出
awk 语法:
awk [options][BEGIN]{program}[END][file]
options:常用命令选项
-F fs 指定描绘一行中数据字段的文件分隔符 默认为空格
-f file 指定读取程序的文件名
-v var=value 定义awk程序中使用的变量和默认值
---- 查看内存使用情况
[root@shell37 ~]# free
total used free shared buff/cache available
Mem: 995896 552648 68988 70896 374260 159112
Swap: 2097148 14080 2083068
------- 查看内存使用情况
[root@shell37 ~]# head -3 /proc/meminfo
MemTotal: 995896 kB
MemFree: 69160 kB
MemAvailable: 159292 kB
2.1)awk对字段(列)的提取
字段提取:提取一个文本中的一列数据并打印输出
字段相关内置变量
$0 表示整行文本
$1 表示文本行中的第一个数据字段
$2 表示文本行中的第二个数据字段
$N 表示文本行中的第N个数据字段
$NF 表示文本行中的最后一个数据字段
[root@shell37 shell]# awk '{print $0}' awktest.txt
1 the quick brown fox jumps over the lazy cat . dog
2 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy cat . dog
5 the quick brown fox jumps over the lazy cat . dog
[root@shell37 shell]# awk '{print $NF}' awktest.txt
dog
dog
dog
dog
dog
[root@shell37 shell]# awk '{print $3}' awktest.txt
quick
quick
quick
quick
quick
2.3)awk对记录(行)的提取
NR: 指定行号
--- NR==3 第三行 全部列 进行打印
[root@shell37 shell]# awk 'NR==3{print $0}' awktest.txt
3 the quick brown fox jumps over the lazy cat . dog
指定分隔符为:
---- 打印第一行
[root@shell37 shell]# head -1 passwd
root:x:0:0:root:/root:/bin/bash
[root@shell37 shell]# cp /etc/passwd ./
---- 打印第一行 ,第一列 写法1 ,写法2
[root@shell37 shell]# awk -F ":" 'NR==1{print $1}' passwd
root
[root@shell37 shell]# awk -F: 'NR==1{print $1}' passwd
root
-----指定分隔符: ,打印第一行 1,2,3 三列
[root@shell37 shell]# awk -F ":" 'NR==1{print $1,$2,$3}' passwd
root x 0
-----指定分隔符: ,打印第一行 1,2,3 三列 并对结果进行格式化
[root@shell37 shell]# awk -F ":" 'NR==1{print $1 "-" $2 "-" $3}' passwd
root-x-0
-----指定分隔符: ,打印第一行 1,2,3 三列 继续格式化
[root@shell37 shell]# awk -F ":" 'NR==1{print "account: " $1, "UID: " $2, "DESC: " $3}' passwd
account: root UID: x DESC: 0
P29_awk高级用法1 24:00
-----截取 /proc/meminfo 中的信息
[root@shell37 shell]# head -3 /proc/meminfo | awk 'NR==1{print $0}'
MemTotal: 995896 kB
[root@shell37 shell]# head -3 /proc/meminfo | awk 'NR==1{print $2}'
995896
验证 BEGIN END
[root@shell37 shell]# awk 'BEGIN{print "hello leo"}{ print $0 } END{ print "bye leo" }' awktest.txt
hello leo
1 the quick brown fox jumps over the lazy cat . dog
2 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy cat . dog
5 the quick brown fox jumps over the lazy cat . dog
bye leo
使用定义变量—最简单版本
[root@shell37 shell]# awk 'BEGIN { name="leo"; print name }'
leo
[root@shell37 shell]# awk 'BEGIN { array[1]="leo"; array[2]=33; print array[1] ,array[2] }'
leo 33
awk 高级用法计算内存使用率
[root@shell37 shell]# head -2 /proc/meminfo
MemTotal: 995896 kB
MemFree: 77104 kB
--- 第一行第二列赋值给t 第二行第二列赋值给f
[root@shell37 shell]# head -2 /proc/meminfo | awk 'NR==1{t=$2} NR==2{f=$2; print (t-f)*100/t}'
92.2996
[root@shell37 shell]# head -2 /proc/meminfo | awk 'NR==1{t=$2} NR==2{f=$2; print (t-f)*100/t "%"}'
92.31%
3.2)awk运算
赋值运算 =
比较运算 > >= == < <= !=
数学运算 + - / % * ++ —
逻辑运算 && ||
匹配运算 ~ !~
[root@shell37 shell]# seq 1 10 > num
[root@shell37 shell]# cat num
1
2
3
4
5
6
7
8
9
10
----- 如果 num 的 第一列>5 就打印当前行
[root@shell37 shell]# awk '$1>5{print $0}' num
6
7
8
9
10
[root@shell37 shell]#
比较运算
[root@shell37 shell]# awk 'BEGIN{print 100+2}'
102
[root@shell37 shell]# awk 'BEGIN{print 100>=1 && 100>=4}'
1
定义一个变量
[root@shell37 shell]# awk -v 'count=0' 'BEGIN{count++; print count;}'
1
精确查找
--- 在 passwd 文件中查找 以: 为分隔符 如果第一个字符为root 就打印 整行
[root@shell37 shell]# awk -F ":" '$1=="root"{print $0}' passwd
root:x:0:0:root:/root:/bin/bash
模糊查找
----- 在 passwd 文件中查找 以: 为分隔符 第一个单词包含ro 就打印当前行
[root@shell37 shell]# awk -F ":" '$1 ~ "ro"{print $0}' passwd
root:x:0:0:root:/root:/bin/bash
chrony:x:993:988::/var/lib/chrony:/sbin/nologin
setroubleshoot:x:990:984::/var/lib/setroubleshoot:/sbin/nologin
3.3)awk 环境变量
变量 | 描述 |
---|---|
FIELDWIDTHS | 以空格分隔的数字列表,用空格定义每个数据字段的精确宽度 |
FS | 输入字段分隔符号 |
OFS | 输出字段分隔符号 |
RS | 输入记录分隔符 |
ORS | 输出记录分隔符号 |
用环境变量FS定义分隔符
[root@shell37 shell]# awk -F ":" '$1 ~ "ro"{print $0}' passwd
root:x:0:0:root:/root:/bin/bash
chrony:x:993:988::/var/lib/chrony:/sbin/nologin
setroubleshoot:x:990:984::/var/lib/setroubleshoot:/sbin/nologin
[root@shell37 shell]# awk 'BEGIN{FS=":"} $1 ~ "ro" {print $0}' passwd
root:x:0:0:root:/root:/bin/bash
chrony:x:993:988::/var/lib/chrony:/sbin/nologin
setroubleshoot:x:990:984::/var/lib/setroubleshoot:/sbin/nologin
OFS 定义列的分隔符
[root@shell37 shell]# awk 'BEGIN{FS=":"} $1 ~ "ro" {print $1,$3,$5}' passwd
root 0 root
chrony 993
setroubleshoot 990
[root@shell37 shell]# awk 'BEGIN{FS=":";OFS="-"} $1 ~ "ro" {print $1,$3,$5}' passwd
root-0-root
chrony-993-
setroubleshoot-990-
[root@shell37 shell]# seq 1 10 > num
[root@shell37 shell]# cat num
1
2
3
4
5
6
7
8
9
10
---- 将num 以“” 作为分隔符 ,然后大一 第 1,2,3 列
[root@shell37 shell]# awk 'BEGIN{RS=""}{print $1,$2,$3}' num
1 2 3
--OFS="\n" 输出到屏幕以后 以回车作为分隔符
[root@shell37 shell]# awk 'BEGIN{RS="";OFS="\n"}{print $1,$2,$3}' num
1
2
3
P30_awk高级语法2 19:27
3.4)流程控制
if判断语句
for循环语句
while循环语句
do…while语句
循环控制
if 联系
[root@shell37 shell]# vim num
1
2
3
4
5
6
7
8
9
10
[root@shell37 shell]# awk '$1>5{print $0}' num
6
7
8
9
10
[root@shell37 shell]# awk '{if($1>5) print $0}' num
6
7
8
9
10
[root@shell37 shell]# awk '{ if($1<5) print $1 ; else print $1*2}' num
1
2
3
4
10
12
14
16
18
20
[root@shell37 shell]# awk -v 'sum=0' '{sum+=$1}END{print sum}' num
55
for while do-while 示例
[root@shell37 shell]# cat num2
60 50 100
150 30 10
70 100 40
[root@shell37 shell]# awk '{ sum=0; for (i=1;i<4;i++) sum+=$i; print sum }' num2
210
190
210
[root@shell37 shell]# awk '{sum=0;i=1; while(i<4){ sum+=$i;i++;} print sum }' num2
210
190
210
[root@shell37 shell]# awk '{ sum=0;i=1; do{sum+=$1} while(sum<150) print sum }' num2
180
150
210
awk 小技巧
ping 3 次,每次间隔2秒钟,如果三次全部成功,显示主机在线,否则显示主机不在线
---- NR 行数
[root@shell37 shell]# awk 'END{print NR}' num2
3
---- 文本最后一行
[root@shell37 shell]# awk 'END{print $0}' num2
70 100 40
----- NF 列数
[root@shell37 shell]# awk 'END {print NF}' num2
3
P31_监测一个主机状态 23:26
[root@shell37 shell]# vim ping_status.sh
#!/bin/bash
# step01 ping test result to var
for ((i=1;i<4;i++)) ; do
if ping -c1 $1 &>/dev/null; then
export ping_count"$i"=1
else
export ping_count"$i"=0
fi
sleep 2
done
# 3 time all success is up
if [ $ping_count1 -eq $ping_count2 ] && [ $ping_count2 -eq $ping_count3 ] && [ $ping_count3 -eq 1 ]; then
echo "$1 is up"
else
echo "$1 is down"
fi
unset ping_count1
unset ping_count2
unset ping_count3
"ping_status.sh" 25L, 461C written
[root@shell37 shell]# bash ping_status.sh 192.168.121.37
192.168.121.37 is up
[root@shell37 shell]# bash ping_status.sh 192.168.121.38
192.168.121.38 is down
P32_监控一个服务端口 20:57
已看,未敲代码 20210308
lsof
lsof (list open files)是一个列出当前系统打开文件的工具。在linux系统环境下,任何事物都可以以文件形式存在,通过文件不仅可以访问常规的数据,还可以访问网络连接和硬件。
[root@shell37 bin]# lsof /usr/bin
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 8109 root cwd DIR 8,3 49152 50331721 /usr/bin
lsof 15683 root cwd DIR 8,3 49152 50331721 /usr/bin
lsof 15684 root cwd DIR 8,3 49152 50331721 /usr/bin
[root@shell37 bin]# lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 6823 root 3u IPv4 43792 0t0 TCP *:ssh (LISTEN)
sshd 6823 root 4u IPv6 43801 0t0 TCP *:ssh (LISTEN)
sshd 8085 root 3u IPv4 53760 0t0 TCP shell37:ssh->192.168.121.1:51987 (ESTABLISHED)
[root@shell37 bin]#
mktemp 创建临时文件
[root@shell37 tmp]# mktemp port.XXX
port.zPB
[root@shell37 tmp]# mktemp port.XXX
port.Dee
[root@shell37 tmp]# ll | grep port
-rw-------. 1 root root 0 Mar 9 05:10 port.Dee
-rw-------. 1 root root 0 Mar 9 05:10 port.o6V
-rw-------. 1 root root 0 Mar 9 05:10 port.zPB
查看某台机器功能是否可用
其他方式
[root@shell37 ~]# systemctl status firewalld ----centOS7 查看服务状态
[root@shell37 ~]# service firewalld status ---- centOS6
[root@shell37 ~]# pa aux ------查看进程是否存在
[root@shell37 bin]# lsof -i:22 -----查看端口是否存在
作者推荐方式
----测试端口是否有相应 有'^]'符号表示成功telnet
[root@shell37 bin]# telnet 192.168.121.38 22
Trying 192.168.121.38...
Connected to 192.168.121.38.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
[root@shell37 bin]# which telnet
/usr/bin/telnet
正式的判断某个端口是否畅通脚本
[root@shell37 shell]# vim telnettest.sh
port_status(){
temp_file=`mktemp port_status.XXX`
#1 judge telnet exist or not
[ ! -x /usr/bin/telnet ] &&echo "telnet command not found " && exit 1
#2 将 Telnet ip port 的结果打印到 port_status.XXX 文件下面
(telnet $1 $2 <<EOF
quit
EOF
) &> $temp_file
#3. search the content of the file ,give the result
if egrep "" $temp_file &> /dev/null; then
echo "$1 $2 is open"
else
echo "$1 $2 is close"
fi
#
rm -f $temp_file
}
port_status $1 $2
[root@shell37 shell]# sh -x telnettest.sh 192.168.121.38 22
+ port_status 192.168.121.38 22
++ mktemp port_status.XXX
+ temp_file=port_status.QiZ
+ '[' '!' -x /usr/bin/telnet ']'
+ egrep '' port_status.QiZ
+ echo '192.168.121.38 22 is open'
192.168.121.38 22 is open
+ rm -f port_status.QiZ
[root@shell37 shell]# sh telnettest.sh 192.168.121.38 22
192.168.121.38 22 is open
P33_监控memory使用率 14:17
free
cat /proc/meminfo 都可以实现监控memory的效果
head -5 *** == cat *** | head -5
[root@shell37 tmp]# cat /proc/meminfo | head -5
MemTotal: 995896 kB
MemFree: 74016 kB
MemAvailable: 235232 kB
Buffers: 0 kB
Cached: 287804 kB
[root@shell37 tmp]# head -5 /proc/meminfo
MemTotal: 995896 kB
MemFree: 75116 kB
MemAvailable: 236352 kB
Buffers: 0 kB
free
[root@shell37 shell]# free -m
total used free shared buff/cache available
Mem: 972 499 72 23 399 239
Swap: 2047 2 2045
[root@shell37 shell]#
meminfo
[root@shell37 tmp]# cat /proc/meminfo
MemTotal: 995896 kB
MemFree: 63636 kB
MemAvailable: 236076 kB
Buffers: 0 kB
.....
01 内存使用率
内存申请顺序 : free cache buffer swap
[root@shell37 tmp]# head -2 /proc/meminfo
MemTotal: 995896 kB
MemFree: 76364 kB
[root@shell37 tmp]# head -2 /proc/meminfo |awk 'NR==1{t=$2} NR==2 {f=$2;print(t-f)*100/t }'
92.5297 ------------------使用率
##02 cache 率
[root@shell37 tmp]# head -5 /proc/meminfo
MemTotal: 995896 kB
MemFree: 74508 kB
MemAvailable: 236368 kB
Buffers: 0 kB
Cached: 288436 kB
[root@shell37 tmp]# head -5 /proc/meminfo |awk 'NR==1{t=$2} NR==5 {c=$2;print c*100/t }'
28.9765
buffer 率
[root@shell37 tmp]# head -5 /proc/meminfo
MemTotal: 995896 kB
MemFree: 67628 kB
MemAvailable: 230044 kB
Buffers: 0 kB
Cached: 295212 kB
[root@shell37 tmp]# head -5 /proc/meminfo |awk 'NR==1{t=$2} NR==4 {b=$2;print b*100/t }'
0
总体统计
[root@shell37 shell]# head -5 /proc/meminfo
MemTotal: 995896 kB
MemFree: 71736 kB
MemAvailable: 220148 kB
Buffers: 0 kB
Cached: 293220 kB
[root@shell37 shell]# vim memorytest.sh
memory_use(){
memory_used=`head -5 /proc/meminfo | awk 'NR==1{t=$2} NR==2 {f=$2; print(t-f)*100/t } '`
memory_cache=`head -5 /proc/meminfo |awk 'NR==1{t=$2} NR==5 {c=$2; print c*100/t } ' `
memory_buffer=` head -5 /proc/meminfo | awk 'NR==1{t=$2} NR==4 {b=$2; print b*100/t}' `
echo "memory_used:$memory_used"
echo "memory_cached:$memory_cache"
echo "memory_bufferd:$memory_buffer"
echo -e "memory_used:$memory_used \t memory_cached:$memory_cache \t memory_bufferd:$memory_buffer "
}
memory_use
[root@shell37 shell]# sh memorytest.sh
memory_used:92.7912
memory_cached:29.4219
memory_bufferd:0
memory_used:93.0165 memory_cached:29.5416 memory_bufferd:0
P34_统计使用内存或CPU前十名进程 25:10
ps ----- 静态任务管理器
top ----- 动态任务管理器
P35_io_队列长度监控 22:34
P36_监控脚本总结 14:45
P36_nginx_install_1 25:26
P38_nginx_install_02 23:30
P39_lamp安装脚本 16:23
P40_mysql_备份脚本 32:06
P41_mysql_备份脚本排错方法 18:30
P42_创建user01-user20用户 23:53
更多推荐
linux shell脚本 编程
发布评论