Shell编程 (Ⅰ)——date、if、case、for、while、break、continue
发布日期:2021-09-11 14:30:12 浏览次数:25 分类:技术文章

本文共 11304 字,大约阅读时间需要 37 分钟。

hot3.png

Shell编程 (Ⅰ)

一、shell脚本介绍

  • shell是一种脚本语言 和传统的开发语言比较,会比较简单
  • shell有自己的语法;可以使用逻辑判断、循环等语法
  • 可以自定义函数,目的就是为了减少重复的代码
  • shell是系统命令的集合
  • shell脚本可以实现自动化运维,能大大增加我们的运维效率

二、shell脚本结构和执行

shell脚本的格式

  • 开头需要加#!/bin/bash
  • 以#开头的行作为解释说明
  • 脚本的名字以.sh结尾,用于区分这是一个shell脚本

示例脚本

[root@ying01 shell]# vim 01.sh #!/bin/bash      //头文件echo "123"       //输出123ls               //打开当前目录touch 1.txt      //创建1.txt文档ls               //再次打开当前目录

执行脚本

[root@ying01 shell]# sh 01.sh 123                               01.sh  01.sh  1.txt [root@ying01 shell]# ls01.sh  1.txt

执行脚本的其他方法:

/bin/sh实际是bash的软连接;其实是真正执行的是bash

[root@ying01 shell]# ls -l /bin/bash         -rwxr-xr-x. 1 root root 960472 8月   3 2017 /bin/bash[root@ying01 shell]# ls -l /bin/shlrwxrwxrwx. 1 root root 4 5月   9 02:21 /bin/sh -> bash[root@ying01 shell]# bash 01.sh12301.sh  01.sh  1.txt

还有一种方式,给脚本文件增加可执行权限,也可以执行;

[root@ying01 shell]# ls -l 01.sh                     //未有执行权-rw-r--r-- 1 root root 41 7月  30 00:37 01.sh[root@ying01 shell]# ls01.sh  1.txt[root@ying01 shell]# /root/shell/01.sh               //权限不够-bash: /root/shell/01.sh: 权限不够[root@ying01 shell]# chmod a+x 01.sh [root@ying01 shell]# ls -l 01.sh                     //具有可执行权-rwxr-xr-x 1 root root 41 7月  30 00:37 01.sh[root@ying01 shell]# /root/shell/01.sh               //绝对路径12301.sh  1.txt01.sh  1.txt [root@ying01 shell]# ./01.sh                         //当前目录下12301.sh  1.txt01.sh  1.txt

查看脚本执行过程:** bash -x 脚本**

[root@ying01 shell]# sh -x 01.sh + echo 123123+ ls01.sh  1.txt+ touch 1.txt+ ls01.sh  1.txt

查看脚本是否语法错误: bash -n 脚本

[root@ying01 shell]# vim 01.sh #!/bin/bashecho "123           //把双引号去掉lstouch 1.txtls[root@ying01 shell]# sh -n 01.sh          //执行 检查语法,发现双引号没有成对出现01.sh:行2: 寻找匹配的 `"' 是遇到了未预期的文件结束符01.sh:行6: 语法错误: 未预期的文件结尾

三、date命令用法

常用格式

  • date +%Y-%m-%d, date +%y-%m-%d 年月日
  • date +%H:%M:%S = date +%T 时间
  • date +%s 时间戳
  • date -d @1504620492
  • date -d "+1day" 一天后
  • date -d "-1 day" 一天前
  • date -d "-1 month" 一月前
  • date -d "-1 min" 一分钟前
  • date +%w, date +%W 星期
root@ying01 shell]# date                    //当前的日期2018年 07月 29日 星期一 09:14:12 CST令[root@ying01 shell]# cal                    //日历      七月 2018     日 一 二 三 四 五 六 1  2  3  4  5  6  7 8  9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31[root@ying01 shell]# date +%Y              //大写Y 年份2018[root@ying01 shell]# date +%y              //小写y 年份18[root@ying01 shell]# date +%m              //小写m 月份07[root@ying01 shell]# date +%Y%m%d          //年月日20180729[root@ying01 shell]# date2018年 07月 30日 星期一 09:16:35 CST[root@ying01 shell]#  date +%F             //大写F 日期格式2018-07-30[root@ying01 shell]# date +%H              //大写H  小时09[root@ying01 shell]# date +%M              //大写M  分钟17[root@ying01 shell]# date +%S              //大写S  秒14[root@ying01 shell]# date +%s              //小写s  时间戳1532913453[root@ying01 shell]# date +%T              //大写T  常规时间09:17:53[root@ying01 shell]# date "+%Y-%m-%d %H:%M:%S %w"     //全部时间列出 年月日 时分秒 星期2018-07-29 09:18:17 7[root@ying01 shell]#

date -d选项用法

[root@ying01 shell]# date -d "-1 day"             //前一天的日期时间2018年 07月 28日 星期六 09:19:12 CST[root@ying01 shell]# date -d "-1 day" +%F         //带格式的 前一天的日期时间2018-07-29[root@ying01 shell]# date -d "-1 month" +%F       // 带格式的 上一个月2018-06-29[root@ying01 shell]# date -d "-1 year" +%F        //带格式的 上一个月2017-07-29[root@ying01 shell]# date -d "-1 hour" +%T        //上一小时的时间08:19:54[root@ying01 shell]# date +%s -d "2017-07-29 09:17:53"  //查看其对应的时间戳1501291073

四、shell脚本中的变量

  • 当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替(比如a=jdjjdjdddx)
  • 使用条件语句时,常使用变量 if [ $a -gt 1 ]; then ... ; fi
  • 引用某个命令的结果时,用变量替代 n=wc -l 1.txt
  • 写和用户交互的脚本时,变量也是必不可少的 read -p "Input a number: " n; echo $n 如果没写这个n,可以直接使用$REPLY
  • 内置变量 $0, $1, $2… $0表示脚本本身,$1 第一个参数,$2 第二个 .... $#表示参数个数
  • 数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]

五、shell脚本中的逻辑判断

if命令的格式:

  • 格式1:if 条件 ; then 语句; fi
  • 格式2:if 条件; then 语句; else 语句; fi
  • 格式3:if …; then … ;elif …; then …; else …; fi

逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格

逻辑判断符号

符号 释义 对应单词
-gt 大于 greater than
-lt 小于 ess than
-ge 大于或等于 greater than or equal
-le 小于或等于 less than or equal
-eq 等于 equality
-ne 不等于 inequality

可以使用 && || 结合多个条件

  • if [ $a -gt 5 ] && [ $a -lt 10 ]; then
  • if [ $b -gt 5 ] || [ $b -lt 3 ]; then
  • 格式1:if 条件 ; then 语句; fi
[root@ying01 shell]# vim if01.sh#!/bin/basha=2if [ $a -lt 3 ]     //变量a小于3的时候,输出OKthen   echo okfi[root@ying01 shell]# sh -x if01.sh + a=2+ '[' 2 -lt 3 ']'+ echo okok
  • 格式2:if 条件; then 语句; else 语句; fi
[root@ying01 shell]# vim if02.sh#!/bin/basha=2if [ $a -lt 3 ]then   echo okelse   echo nookfi[root@ying01 shell]# sh -x if02.sh + a=2+ '[' 2 -lt 3 ']'+ echo okok
  • 格式3:if …; then … ;elif …; then …; else …; fi
[root@ying01 shell]# vim if03.sh#!/bin/bashread -p "请输入考试分数:" aif [ $a -lt 60 ]then   echo "太差劲了!重考,未通过考试!"elif [ $a -gt 60 ] && [ $a -lt 85 ]then   echo "还行吧!通过考试,成绩良好!"else   echo "恭喜你!通过考试,成绩优秀! "fi[root@ying01 shell]# sh if03.sh请输入考试分数:48太差劲了!重考,未通过考试![root@ying01 shell]# sh if03.sh请输入考试分数:65还行吧!通过考试,成绩良好![root@ying01 shell]# sh if03.sh请输入考试分数:99恭喜你!通过考试,成绩优秀!

六、文件目录属性判断

  • [ -f file ]判断是否是普通文件,且存在
  • [ -d file ] 判断是否是目录,且存在
  • [ -e file ] 判断文件或目录是否存在
  • [ -r file ] 判断文件是否可读
  • [ -w file ] 判断文件是否可写
  • [ -x file ] 判断文件是否可执行

判断是否是普通文件,且存在: [ -f file ]

[root@ying01 shell]# vim file1.sh#! /bin/bashf="/root/ceshi"if [ -f $f ]then        echo $f existelse        touch $ffi[root@ying01 shell]# sh -x file1.sh+ f=/root/ceshi+ '[' -f /root/ceshi ']'+ touch /root/ceshi[root@ying01 shell]# sh -x file1.sh+ f=/root/ceshi+ '[' -f /root/ceshi ']'+ echo /root/ceshi exist/root/ceshi exist

判断是否是目录,且存在: [ -d file ]

[root@ying01 shell]# vim file2.sh#! /bin/bashf="/root/ceshi"if [ -d $f ]then        echo $f existelse        mkdir $f        ls -ld $ffi[root@ying01 shell]# sh -x file2.sh+ f=/root/ceshi+ '[' -d /root/ceshi ']'+ mkdir /root/ceshi+ ls -ld /root/ceshidrwxr-xr-x 2 root root 6 7月  29 11:19 /root/ceshi

并且 &&

f="/root/ceshi"[ -f $f ] && rm -f $f     //前一条命令执行成功才会继续执行之后的命令等同于下面的表达方式if [ -f $f ]     then      rm -rf $ffi

或者 ||

f="/root/ceshi"[ -f $f ] || touch $f    //前面命令不成功时,执行后面的命令等同于下面的表达方式if [ ! -f $f ]        //  “!”表示了如果这条命令不成功,就往下执行then      touch $ffi

七、if 特殊用法

  • if [ -z "$a" ] 逻辑条件是:变量a的值为空
  • if [ -n "$a" ] 逻辑条件是:变量a的值不为空
  • if grep -q '123' 1.txt; then 逻辑条件是:1.txt中含有'123'的行
  • if [ ! -e file ]; then 逻辑条件是:文件不存在

圆括号与方括号的区别:

  • if (($a<1)); then … 等同于 if [ $a -lt 1 ]; then…
  • [ ] 中不能使用<,>,==,!=,>=,<=这样的符号

常见的一些用法注意:

  • if -z或者if -n 都不能作用在文件上,只能作用在变量上。
  • if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
  • !-z=-n
  • !-n=-z
[root@ying01 shell]# vim  file3.sh#! /bin/bashn=`wc -l /etc/passwd |awk '{print $1}'`  //输出/etc/passwd的行数if [ -z "$n" ]                           //条件:是否为空then    echo error    exitelif [ $n -gt 20 ]                      //条件:n是否大于20then    echo OKfi[root@ying01 shell]# sh -x  file3.sh++ wc -l /etc/passwd++ awk '{print $1}'+ n=46+ '[' -z 46 ']'+ '[' 46 -gt 20 ']'+ echo OKOK

if [ -n "$a" ] 表示当变量a的值不为空,或者说这个文件内容不为空

-n 判断变量的时候,需要用""双引号引起来,若是文件的时候,则不需要用双引号引起来

[root@ying01 shell]# if [ -n file3.sh ]; then echo ok; fiok[root@ying01 shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fib is null

八、case 判断

case  变量名 in value1)  command  ;;value2)  command  ;;*)  commond  ;;esac

如果case中的某个value是一样的,我们可以这样写:

在case程序中,可以在条件中使用 |,表示或的意思,  比如2|3)         command      ;;

脚本案例:

#!/bin/bashread -p "Please input a number: " n     //让用户输入一个数字if [ -z "$n" ]              //判断用户有没有输入then    echo "Please input a number."    exit 1fin1=`echo $n|sed 's/[0-9]//g'`       //检查用户输入的是不是全部是数字,不是数字就置空if [ -n "$n1" ]then echo "Please just input a number, without any other words." exit 1fiif [ $n -lt 60 ] && [ $n -ge 0 ]        //经过如上的筛选,我们来判断输入数字属于哪个范围,并且把值交给tagthen    tag=1elif [ $n -ge 60 ] && [ $n -lt 80 ]then    tag=2elif [ $n -ge 80 ]  && [ $n -lt 90 ]then    tag=3elif [ $n -ge 90 ] && [ $n -le 100 ]then    tag=4else     tag=0  //大于100的情况ficase $tag in        //根据如上得到的值,进行判断    1)    echo "you didn't pass the exam!"        ;;    2)        echo "good!"        ;;    3)        echo "very good!"        ;;    4)        echo "perfect!!!"        ;;    *)        echo "Pls input a number range 0-100."        ;; esac

九、for循环

重复执行一系列命令在 编程中很常见。通常你需要重复一组命令直到达到某个特定条件,比如处理某个目录下的所有文件、系统上的所有用户或者是某个文本文件中的所有行。

常见的两种循环,在脚本中普遍被用到。

  • for循环
  • while循环

for循环演示:

累加求和

[root@ying01 shell]# vim sum01.sh#!/bin/bashsum=0for i in `seq 1 3`do   sum=$[ $sum+$i ]   echo $idoneecho SUM=$sum[root@ying01 shell]# sh -x  sum01.sh + sum=0++ seq 1 3+ for i in '`seq 1 3`'+ sum=1+ echo 11+ for i in '`seq 1 3`'+ sum=3+ echo 22+ for i in '`seq 1 3`'+ sum=6+ echo 33+ echo SUM=6SUM=6

遍历一个目录的目录或者文件

[root@ying01 shell]# vim for02.sh#!/bin/bashcd /root/100                                //进入到目录for a in `ls /root/100`                     //遍历此目录do   [ -d $a ] && ls $a                        # 判断是否是目录,并列出其下文件和子目录   if [ -d $a ]   then       echo $a       ls $a   fidone[root@ying01 shell]# ls /root/10010  3[root@ying01 shell]# sh -x for02.sh + cd /root/100++ ls /root/100+ for a in '`ls /root/100`'+ '[' -d 10 ']'+ ls 10\#  4.txt+ '[' -d 10 ']'+ echo 1010+ ls 10\#  4.txt+ for a in '`ls /root/100`'+ '[' -d 3 ']'+ ls 32.txt+ '[' -d 3 ']'+ echo 33+ ls 32.txt[root@ying01 shell]# sh for02.sh \#  4.txt10\#  4.txt2.txt32.txt

特殊for循环示例:list循环时,会以空格或回车符作为分隔符了

十、 while循环

语法 while 条件; do … ; done

  • 案例1:

每隔1分钟检查一下系统负载,当系统的负载大于10的时候,发一封邮件(监控脚本) 最小单元是任务计划 cron

[root@ying01 shell]# vim while01.sh #!/bin/bashwhile :# 冒号 : 表示死循环的意思,或者1,或者 true都是死循环do    load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`    if [ $load -gt 10 ]    then           /usr/local/sbin/mail.py txwd188@126.com "load high" "$load"    fi    sleep 30#休眠30秒,因为检查系统负载,不需要一直去检查,过一会再看done[root@ying01 shell]# sh -x while01.sh + :++ w++ cut -d. -f1++ awk -F 'load average: ' '{print $2}'++ head -1+ load=0+ '[' 0 -gt 10 ']'+ sleep 30

代码名词释义

  • w :查看系统负载 ;
  • uptime 可以直接显示 w 系统负载的第一行,就可以省去 head -1
  • head -1 //取第一行
  • awk -F 'load average: ' '{print $2}' // 以'load average: '分隔,输出第二段
  • cut -d . -f1 // 以 . 分隔 取第一段
  • while循环案例2

在循环过程过,需要用户输入一个数字;输入的不是数字,是数字,输入为空;回应相应的结果

[root@ying01 shell]# vim while02.sh#!/bin/bashwhile :do    read -p "Please input a number: " n    if [ -z "$n" ]    then        echo "you need input sth."        continue#continue 重新回到循环    fi    n1=`echo $n|sed 's/[0-9]//g'`    if [ -n "$n1" ]    then        echo "you just only input numbers."        continue    fi    break#break  退出循环doneecho $n[root@ying01 shell]# sh  while02.sh Please input a number: k       you just only input numbers.Please input a number: !you just only input numbers.Please input a number: 55

十一、break跳出循环

break 常用于循环语句中,跳出整个循环语句,直接结束所有循环。

[root@ying01 shell]# vim break01.sh#!/bin/bashfor i in `seq 1 5`do    echo A=$i    if [ $i -eq 3 ]#比较数字,用-eq ;若是比较的是字符串,那需要用 ==    then        break    fi    echo B=$idoneecho C=$i[root@ying01 shell]# sh break01.sh A=1B=1A=2B=2A=3C=3

十二、continue结束本次循环

忽略continue之下的代码,直接进行下一次循环

[root@ying01 shell]# vim contiue01.sh#!/bin/bashfor i in `seq 1 5`do    echo A=$i    if [ $i -eq 3 ]    then        continue    fi    echo B=$idoneecho C=$i[root@ying01 shell]# sh contiue01.sh  //注意没有B=3行A=1B=1A=2B=2A=3A=4B=4A=5B=5C=5

十三、exit退出整个脚本

exit可以定义退出的数值,可以用于确定脚本运行到什么地方的时候,结束

[root@ying01 shell]# vim exit01.sh#!/bin/bashfor i in `seq 1 5`do    echo A=$i    if [ $i -eq 3 ]    then        exit    fi    echo B=$idoneecho C=$i[root@ying01 shell]# sh exit01.sh   //直接从A=3退出;A=1B=1A=2B=2A=3

转载于:https://my.oschina.net/u/3851633/blog/1918777

转载地址:https://blog.csdn.net/weixin_34413802/article/details/92416493 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:大数据之Linux早课10.7
下一篇:下一个十年:练好内功被集成的弹性计算

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年03月13日 22时49分14秒