shell编程综合练习(个人学习记录)

shell编程

until 循环

until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反。一般while循环优于until循环,但在某些时候,也只是极少数情况下,until 循环更加有用

# until 循环格式为:
   until command
   do
   Statement(s) to be executed until command is true
   done


# command 一般为条件表达式,如果返回值为 false,则继续执行循环体内的语句,否则跳出循环


# 例 使用 until 命令输出 0 ~ 9 的数字:

[root@localhost test]# vim until.sh
[root@localhost test]# cat until.sh
#!/bin/bash
a=0
until [ ! $a -lt 10 ]
do
        echo $a
        a=`expr $a + 1`
done
[root@localhost test]# chmod +x until.sh
[root@localhost test]# sh until.sh
0
1
2
3
4
5
6
7
8
9

跳出循环

在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,像大多数编程语言一样,Shell也使用 breakcontinue 来跳出循环

break命令

break命令允许跳出所有循环(终止执行后面的所有循环)

# 例:脚本进入死循环直至用户输入数字大于5。要跳出这个循环,返回到shell提示符下,就要使用break命令

# 代码如下:
[root@localhost test]# vim break.sh
[root@localhost test]# chmod +x break.sh
[root@localhost test]# cat break.sh
#!/bin/bash
while :
do
        echo -n "Input a number between 1 to 5: "
        read aNum
        case $aNum in
                1|2|3|4|5) echo "Your number is $aNum!"
                ;;
                *) echo "You do not select a number between 1 to 5,game is over !"
                   break
                ;;
        esac
done

# 运行效果如下: 
[root@localhost test]# sh break.sh
Input a number between 1 to 5: 1
Your number is 1!
Input a number between 1 to 5: 2
Your number is 2!
Input a number between 1 to 5: 3
Your number is 3!
Input a number between 1 to 5: 4
Your number is 4!
Input a number between 1 to 5: 5
Your number is 5!
Input a number between 1 to 5: 6
You do not select a number between 1 to 5,game is over !

# 在嵌套循环中,break 命令后面还可以跟一个整数,表示跳出第几层循环。
#例如:
break n
表示跳出第 n 层循环

# 代码如下
[root@localhost test]# vim break-2.sh
[root@localhost test]# chmod +x break-2.sh
[root@localhost test]# cat break-2.sh
#!/bin/bash
for var1 in 1 2 3
do
        for var2 in 0 5
        do
                if [ $var1 -eq 2 -a $var2 -eq 0 ]
                then
                        break 2
                else
                        echo "$var1 $var2"
                fi
        done
done

# 运行结果如下
[root@localhost test]# sh break-2.sh
1 0
1 5

continue 命令

continue命令与break命令类似,只有一点差别,它不会跳出所有循环,仅仅跳出当前循环

# 例:
# 代码入下:
[root@localhost test]# vim continue.sh
[root@localhost test]# chmod +x continue.sh
[root@localhost test]# cat continue.sh
#!/bin/bash
while :
do
        read -p "Input a number between 1 to 5: " aNum
        case $aNum in
                1|2|3|4|5) echo "Your number is $aNum!"
                ;;
                *) echo "You do not select a number between 1 to 5!"
                        continue
                        echo "Game is over!"
                ;;
        esac
done

# 运行效果如下:
[root@localhost test]# sh continue.sh
Input a number between 1 to 5: 1
Your number is 1!
Input a number between 1 to 5: 2
Your number is 2!
Input a number between 1 to 5: 8
You do not select a number between 1 to 5!
Input a number between 1 to 5: 5
Your number is 5!
Input a number between 1 to 5: 8
You do not select a number between 1 to 5!
Input a number between 1 to 5: 9
You do not select a number between 1 to 5!
Input a number between 1 to 5: ^C
[root@localhost test]#

# 运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句
echo "Game is over!"
永远不会被执行

Shell函数

函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高。像其他编程语言一样,Shell 也支持函数。Shell 函数必须先定义后使用

# Shell 函数的定义格式如下:

function_name  ( ) {
    
    

  list of commands

  [ return value ]

}
  • 函数返回值,可以显式增加return语句;如果不加,会将最后一条命令运行结果作为返回值

  • Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败

  • 在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数…

# 例:代码如下

[root@localhost test]# vim funWithParam
[root@localhost test]# chmod +x funWithParam
[root@localhost test]# cat funWithParam
#!/bin/bash
funWithParam(){
    
    
        echo "The value of the first parameter is $1 !"
        echo "The value of the second parameter is $2 !"
        echo "The value of the tenth parameter is $10 !"
        echo "The value of the tenth parameter is ${10} !"
        echo "The value of the eleventh parameter is ${11} !"
        echo "The amount of the  parameter is $# !"
        echo "The string of the  parameter is $* !"
}

# 运行如下:
[root@localhost test]# source funWithParam
[root@localhost test]# funWithParam 1 2 3 5 6 7 4 20 21
The value of the first parameter is 1 !
The value of the second parameter is 2 !
The value of the tenth parameter is 10 !
The value of the tenth parameter is  !
The value of the eleventh parameter is  !
The amount of the  parameter is 9 !
The string of the  parameter is 1 2 3 5 6 7 4 20 21 !

特殊变量

image-20230325095616142

输入输出重定向

从标准输入设备(stdin)获取输入,将结果输出到标准输出设备(stdout)显示。一般情况下,标准输入设备就是键盘,标准输出设备就是终端,即显示器

输出重定向

命令的输出不仅可以是显示器,还可以很容易的转移向到文件,这被称为输出重定向

# 例:
[root@localhost test]# who
root     pts/0        2023-03-25 09:00 (192.168.180.1)
[root@localhost test]# who > username.txt
[root@localhost test]# cat username.txt
root     pts/0        2023-03-25 09:00 (192.168.180.1)

# 注意:
 >   输出重定向会覆盖文件内容
 >>  输出重定向追加到文件末尾
 
 # 追加
[root@localhost test]# pwd >> username.txt
[root@localhost test]# cat username.txt
root     pts/0        2023-03-25 09:00 (192.168.180.1)
/root/test

输入重定向

# 格式
command  <  file


# 例:计算 username文件中的行数

[root@localhost test]# wc -l < username.txt
2

重定向

  • 一般情况下,每个 Unix/Linux 命令运行时都会打开三个文件:

  • 标准输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据。

  • 标准输出文件(stdout):stdout 的文件描述符为1,Unix程序默认向stdout输出数据。

  • 标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息。

1.默认情况下,command  >  file 将 stdout 重定向到 file,command  < file 将stdin 重定向到 file
# 如果希望 stderr 重定向到 file,可以这样写:
$command 2 > file

# 如果希望 stderr 追加到 file 文件末尾,可以这样写:
$command 2 >> file
2 表示标准错误文件(stderr)

# 如果希望将 stdout 和 stderr 合并后重定向到 file,可以这样写:
$command >> file 2>&1$command > file 2>&1

# 如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null:
$ command > /dev/null

# 如果希望屏蔽 stdout 和 stderr,可以这样写:
command > /dev/null 2>&1


# /dev/null: 表示 的是一个黑洞,通常用于丢弃不需要的数据输出, 或者用于输入流的空文件

# 例:将无用的输出流写入到黑洞丢弃。错误信息定位到黑洞curl -l  www.baidu.com 2>/dev/null 
将标准输出信息定位输出到/dev/null黑洞
curl -l www.baidu.com 1> /dev/null 


# 在书写定时任务,规范的写法就是将所有定时任务脚本结尾加上>/dev/null 2>&1,让所有的输出流(包括错误的和正确的)都定向到空设备丢弃。例:
 00 01 * * * /bin/sh/server/scripts/mysqlbak.sh  >/dev/null  2>&1

Shell实战

监控centos7运行状态

利用vmstat工具监控CPU详细信息,然后基于/proc/stat计算CPU利用率进行监控,超过80报警并提取出占用cpu最高的前十进程

vmstat是Linux系统监控工具,使用vmstat命令可以得到关于进程、内存、内存分页、堵塞IO、traps及CPU活动的信息

image-20230325103415341

  • r:运行队列中的进程数;

  • b:等待IO的进程数

  • swpd:已用虚拟内存大小(k);free:空闲内存大小;buff:已用缓冲大小;cache:已用缓存大小。

  • si:每秒从交换区写入内存的大小(kb/s);

  • so:每秒从内存写入交换分区的大小

  • bi:每秒读取的块数;bo每秒写入的块数。

  • in:每秒中断数,包括时钟中断;cs:每秒上下文切换数。

  • us(user time):用户进程执行消耗cpu时间;

  • sy(system time):系统进程执行消耗cpu时间;

  • id:空闲时间(包括IO等待时间);

  • wa:等待IO时间。

/proc/stat文件

该文件包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累计到当前时刻。可以利用其中信息计算cpu的利用率


[root@localhost test]# cat /proc/stat
cpu  372 0 1266 2494149 125 0 25 0 0 0
cpu0 70 0 492 623135 14 0 15 0 0 0
cpu1 136 0 316 623496 96 0 6 0 0 0
cpu2 79 0 212 623795 8 0 1 0 0 0
cpu3 86 0 246 623722 6 0 2 0 0 0
intr 525088 73 10 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 896071
btime 1679705900
processes 1729
procs_running 1
procs_blocked 0
softirq 312789 1 121008 1253 13800 11060 0 873 100236 0 64558

编写脚本

[root@localhost test]# vim vmstat.sh
[root@localhost test]# chmod +x vmstat.sh

# 代码如下:
[root@localhost test]# cat vmstat.sh
#!/bin/bash
CPU_us=$(vmstat | awk '{print $13}' | sed -n '$p')
CPU_sy=$(vmstat | awk '{print $14}' | sed -n '$p')
CPU_id=$(vmstat | awk '{print $15}' | sed -n '$p')
CPU_wa=$(vmstat | awk '{print $16}' | sed -n '$p')
CPU_st=$(vmstat | awk '{print $17}' | sed -n '$p')

CPU1=`cat /proc/stat | grep 'cpu' | awk '{print $2" " $3" " $4" " $5" " $6" "$7" "$8}'`
sleep 5
CPU2=`cat /proc/stat | grep 'cpu' | awk '{print $2" " $3" " $4" " $5" " $6" "$7" "$8}'`
IDLE1=`echo $CPU1 | awk '{print $4}'`
IDLE2=`echo $CPU2 | awk '{print $4}'`
CPU1_TOTAL=`echo $CPU1 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`
CPU2_TOTAL=`echo $CPU2 | awk '{print $1+$2+$3+$4+$5+$6+$7}'`
IDLE=`echo "$IDLE2-$IDLE1" | bc`
CPU_TOTAL=`echo "$CPU2_TOTAL - $CPU1_TOTAL" | bc`
RATE=`echo "scale=4;($CPU_TOTAL-$IDLE)/$CPU_TOTAL*100" | bc | awk '{printf "%.2f",$1}'`
echo -e "us=$CPU_us\tsy=$CPU_sy\tid=$CPU_id\twa=$CPU_wa\tst=$CPU_st"
echo "CPU_RATE:${RATE}%"
CPU_RATE=`echo $RATE | cut -d. -f1`
if  [ $CPU_RATE -ge 80 ]
then    echo "CPU Warn"
        ps aux | grep -v USER | sort -rn -k3 | head
fi


# 运行效果如下

[root@localhost test]# sh vmstat.sh
us=0    sy=0    id=100  wa=0    st=0
CPU_RATE:0.04%
[root@localhost test]#

free命令监控系统内存

[root@localhost test]# vim free.sh
[root@localhost test]# chmod +x free.sh

# 代码如下:
[root@localhost test]# cat free.sh
#!/bin/bash
total=$(free -m | sed -n '2p' | awk '{print $2}')
used=$(free -m | sed -n '2p' | awk '{print $3}')
free=$(free -m | sed -n '2p' | awk '{print $4}')
shared=$(free -m | sed -n '2p' | awk '{print $5}')
buff=$(free -m | sed -n '2p' | awk '{print $6}')
cached=$(free -m | sed -n '2p' | awk '{print $7}')
rate=`echo "scale=2;$used/$total" | bc | awk -F. '{print $2}'`
echo -e "total\tused\tfree\tshared\tbuffer\tavailable"
echo -e "${total}M\t${used}M\t${free}M\t${shared}M\t${buff}M\t${cached}M\nrate:${rate}$"
if      [ $rate -ge 80 ]
then    echo "Memory Warn"
    ps aux | grep -v USER | sort -rn -k4 | head
fi



# 运行如下:
[root@localhost test]# sh free.sh
total   used    free    shared  buffer  available
1819M   222M    1339M   9M      256M    1447M
rate:12$

猜你喜欢

转载自blog.csdn.net/huz1Vn/article/details/129768073