当前位置: 首页 > news >正文

成都程序员网站设计本官方网站广告

成都程序员网站,设计本官方网站广告,婚庆公司排名前十,雏光 网络推广 网站建设shell编程综合练习#xff08;个人学习记录#xff09; shell编程until 循环跳出循环1. break命令2. continue 命令 Shell函数特殊变量输入输出重定向输出重定向输入重定向重定向 Shell实战监控centos7运行状态/proc/stat文件编写脚本free命令监控系统内存 shell编程 until … shell编程综合练习个人学习记录 shell编程until 循环跳出循环1. break命令2. continue 命令 Shell函数特殊变量输入输出重定向输出重定向输入重定向重定向 Shell实战监控centos7运行状态/proc/stat文件编写脚本free命令监控系统内存 shell编程 until 循环 until 循环执行一系列命令直至条件为 true 时停止。until 循环与 while 循环在处理方式上刚好相反。一般while循环优于until循环但在某些时候也只是极少数情况下until 循环更加有用 # until 循环格式为until commanddoStatement(s) to be executed until command is truedone# command 一般为条件表达式如果返回值为 false则继续执行循环体内的语句否则跳出循环# 例 使用 until 命令输出 0 ~ 9 的数字[rootlocalhost test]# vim until.sh [rootlocalhost test]# cat until.sh #!/bin/bash a0 until [ ! $a -lt 10 ] doecho $aaexpr $a 1 done [rootlocalhost test]# chmod x until.sh [rootlocalhost test]# sh until.sh 0 1 2 3 4 5 6 7 8 9跳出循环 在循环过程中有时候需要在未达到循环结束条件时强制跳出循环像大多数编程语言一样Shell也使用 break 和 continue 来跳出循环 1. break命令 break命令允许跳出所有循环终止执行后面的所有循环 # 例脚本进入死循环直至用户输入数字大于5。要跳出这个循环返回到shell提示符下就要使用break命令# 代码如下 [rootlocalhost test]# vim break.sh [rootlocalhost test]# chmod x break.sh [rootlocalhost test]# cat break.sh #!/bin/bash while : doecho -n Input a number between 1 to 5: read aNumcase $aNum in1|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# 运行效果如下 [rootlocalhost 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 层循环# 代码如下 [rootlocalhost test]# vim break-2.sh [rootlocalhost test]# chmod x break-2.sh [rootlocalhost test]# cat break-2.sh #!/bin/bash for var1 in 1 2 3 dofor var2 in 0 5doif [ $var1 -eq 2 -a $var2 -eq 0 ]thenbreak 2elseecho $var1 $var2fidone done# 运行结果如下 [rootlocalhost test]# sh break-2.sh 1 0 1 5 2. continue 命令 continue命令与break命令类似只有一点差别它不会跳出所有循环仅仅跳出当前循环 # 例 # 代码入下 [rootlocalhost test]# vim continue.sh [rootlocalhost test]# chmod x continue.sh [rootlocalhost test]# cat continue.sh #!/bin/bash while : doread -p Input a number between 1 to 5: aNumcase $aNum in1|2|3|4|5) echo Your number is $aNum!;;*) echo You do not select a number between 1 to 5!continueecho Game is over!;;esac done# 运行效果如下 [rootlocalhost 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 [rootlocalhost test]## 运行代码发现当输入大于5的数字时该例中的循环不会结束语句 echo Game is over! 永远不会被执行 Shell函数 函数可以让我们将一个复杂功能划分成若干模块让程序结构更加清晰代码重复利用率更高。像其他编程语言一样Shell 也支持函数。Shell 函数必须先定义后使用 # Shell 函数的定义格式如下function_name ( ) {list of commands[ return value ]}函数返回值可以显式增加return语句如果不加会将最后一条命令运行结果作为返回值 Shell 函数返回值只能是整数一般用来表示函数执行成功与否0表示成功其他值表示失败 在Shell中调用函数时可以向其传递参数。在函数体内部通过 $n 的形式来获取参数的值例如$1表示第一个参数$2表示第二个参数… # 例代码如下[rootlocalhost test]# vim funWithParam [rootlocalhost test]# chmod x funWithParam [rootlocalhost 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 $* ! }# 运行如下 [rootlocalhost test]# source funWithParam [rootlocalhost 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 ! 特殊变量 输入输出重定向 从标准输入设备(stdin)获取输入将结果输出到标准输出设备(stdout)显示。一般情况下标准输入设备就是键盘标准输出设备就是终端即显示器 输出重定向 命令的输出不仅可以是显示器还可以很容易的转移向到文件这被称为输出重定向 # 例 [rootlocalhost test]# who root pts/0 2023-03-25 09:00 (192.168.180.1) [rootlocalhost test]# who username.txt [rootlocalhost test]# cat username.txt root pts/0 2023-03-25 09:00 (192.168.180.1)# 注意 输出重定向会覆盖文件内容 输出重定向追加到文件末尾# 追加 [rootlocalhost test]# pwd username.txt [rootlocalhost test]# cat username.txt root pts/0 2023-03-25 09:00 (192.168.180.1) /root/test输入重定向 # 格式 command file# 例:计算 username文件中的行数[rootlocalhost test]# wc -l username.txt 2重定向 一般情况下每个 Unix/Linux 命令运行时都会打开三个文件 标准输入文件(stdin)stdin的文件描述符为0Unix程序默认从stdin读取数据。 标准输出文件(stdout)stdout 的文件描述符为1Unix程序默认向stdout输出数据。 标准错误文件(stderr)stderr的文件描述符为2Unix程序会向stderr流中写入错误信息。 1.默认情况下command file 将 stdout 重定向到 filecommand file 将stdin 重定向到 file # 如果希望 stderr 重定向到 file可以这样写 $command 2 file# 如果希望 stderr 追加到 file 文件末尾可以这样写 $command 2 file 2 表示标准错误文件(stderr)# 如果希望将 stdout 和 stderr 合并后重定向到 file可以这样写 $command file 21 或 $command file 21# 如果希望执行某个命令但又不希望在屏幕上显示输出结果那么可以将输出重定向到 /dev/null $ command /dev/null# 如果希望屏蔽 stdout 和 stderr可以这样写 command /dev/null 21# /dev/null: 表示 的是一个黑洞通常用于丢弃不需要的数据输出 或者用于输入流的空文件# 例:将无用的输出流写入到黑洞丢弃。错误信息定位到黑洞curl -l www.baidu.com 2/dev/null 将标准输出信息定位输出到/dev/null黑洞 curl -l www.baidu.com 1 /dev/null # 在书写定时任务规范的写法就是将所有定时任务脚本结尾加上/dev/null 21让所有的输出流包括错误的和正确的都定向到空设备丢弃。例00 01 * * * /bin/sh/server/scripts/mysqlbak.sh /dev/null 21 Shell实战 监控centos7运行状态 利用vmstat工具监控CPU详细信息然后基于/proc/stat计算CPU利用率进行监控超过80报警并提取出占用cpu最高的前十进程 vmstat是Linux系统监控工具使用vmstat命令可以得到关于进程、内存、内存分页、堵塞IO、traps及CPU活动的信息 r运行队列中的进程数 b等待IO的进程数 swpd已用虚拟内存大小kfree空闲内存大小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的利用率 [rootlocalhost 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 0 0 0 0 0 0 1 0 0 0 16 0 0 6218 2800 8124 68 13796 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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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 ctxt 896071 btime 1679705900 processes 1729 procs_running 1 procs_blocked 0 softirq 312789 1 121008 1253 13800 11060 0 873 100236 0 64558编写脚本 [rootlocalhost test]# vim vmstat.sh [rootlocalhost test]# chmod x vmstat.sh# 代码如下 [rootlocalhost 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)CPU1cat /proc/stat | grep cpu | awk {print $2 $3 $4 $5 $6 $7 $8} sleep 5 CPU2cat /proc/stat | grep cpu | awk {print $2 $3 $4 $5 $6 $7 $8} IDLE1echo $CPU1 | awk {print $4} IDLE2echo $CPU2 | awk {print $4} CPU1_TOTALecho $CPU1 | awk {print $1$2$3$4$5$6$7} CPU2_TOTALecho $CPU2 | awk {print $1$2$3$4$5$6$7} IDLEecho $IDLE2-$IDLE1 | bc CPU_TOTALecho $CPU2_TOTAL - $CPU1_TOTAL | bc RATEecho scale4;($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_RATEecho $RATE | cut -d. -f1 if [ $CPU_RATE -ge 80 ] then echo CPU Warnps aux | grep -v USER | sort -rn -k3 | head fi# 运行效果如下[rootlocalhost test]# sh vmstat.sh us0 sy0 id100 wa0 st0 CPU_RATE:0.04% [rootlocalhost test]#free命令监控系统内存 [rootlocalhost test]# vim free.sh [rootlocalhost test]# chmod x free.sh# 代码如下 [rootlocalhost 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}) rateecho scale2;$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 Warnps aux | grep -v USER | sort -rn -k4 | head fi# 运行如下 [rootlocalhost test]# sh free.sh total used free shared buffer available 1819M 222M 1339M 9M 256M 1447M rate:12$喜欢水星记 shell编程综合练习个人学习记录
http://www.pierceye.com/news/591571/

相关文章:

  • 高端网站设计有哪些企业网站开发php
  • php网站模块网站建设协议 模板
  • 怎么做像知乎一样的网站网络营销案例分析模板
  • 网站开发 h5 h4网站运作模式
  • 竞彩网站开发那些市区做网站群
  • 建投五公司网站找苏州网站建设
  • 徐州IT兼职网站开发中国做陶壶的网站有哪些
  • WordPress模板转换typecho郑州搜索引擎优化
  • 个人怎么做贷款网站什么是网络营销产品
  • 网站你的后台管理系统用什么做外包公司与劳务派遣区别
  • 设计师资源网站价格低油耗低的车
  • 中山快速做网站服务wordpress建站ftp
  • 做酒类直供网站行吗网站seo检测报告
  • 南昌建站费用网站建设出错1004
  • 十堰做网站的公司阿里巴巴外贸网站论坛
  • 品牌网站建设代理网站建设公司易下拉软件
  • 移动网站模板响应式网站开发教程pdf
  • 怎么设计网站内容小程序seo帝搜软件sem880官网
  • 十堰秦楚网 十堰新闻门户网站wordpress 点赞 开启
  • 做外贸网站需要注意些什么手续安阳吧贴吧
  • 国外申请域名的网站百度标记号码认证平台
  • 专门做淘宝代运营的网站支付建设网站的费用什么科目
  • 天津企业设计网站建设建个网站做外贸
  • 申请永久网站空间wordpress论坛采集
  • 网站如何做竞价佛山新网站建设机构
  • 网站建设费可以一次性冲费用吗学校门户网站作用
  • 手机上怎么制作网站音乐网站如何建立
  • 新乡企业网站建设公司寮步东莞网站建设
  • wordpress中国网站排名如何加入广告联盟赚钱
  • 济宁网站建设培训学校wordpress导入表单