Shell编程 (Ⅰ)——date、if、case、for、while、break、continue

Shell编程 (Ⅰ)

1、shell脚本介绍

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

2、shell脚本结构和执行

shell脚本的格式shell

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

示例脚本编程

[root@ying01 shell]# vim 01.sh 


#!/bin/bash      //头文件
echo "123"       //输出123
ls               //打开当前目录
touch 1.txt      //建立1.txt文档
ls               //再次打开当前目录

执行脚本vim

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

执行脚本的其余方法:bash

/bin/sh实际是bash的软链接;实际上是真正执行的是bashless

[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/sh
lrwxrwxrwx. 1 root root 4 5月   9 02:21 /bin/sh -> bash
[root@ying01 shell]# bash 01.sh
123
01.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]# ls
01.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               //绝对路径
123
01.sh  1.txt
01.sh  1.txt 
[root@ying01 shell]# ./01.sh                         //当前目录下
123
01.sh  1.txt
01.sh  1.txt

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

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

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

[root@ying01 shell]# vim 01.sh 

#!/bin/bash
echo "123           //把双引号去掉
ls
touch 1.txt
ls


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

3、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 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 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]# date
2018年 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

4、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]

5、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/bash
a=2
if [ $a -lt 3 ]     //变量a小于3的时候,输出OK
then
   echo ok
fi

[root@ying01 shell]# sh -x if01.sh 
+ a=2
+ '[' 2 -lt 3 ']'
+ echo ok
ok
  • 格式2:if 条件; then 语句; else 语句; fi
[root@ying01 shell]# vim if02.sh

#!/bin/bash
a=2
if [ $a -lt 3 ]
then
   echo ok
else
   echo nook
fi

[root@ying01 shell]# sh -x if02.sh 
+ a=2
+ '[' 2 -lt 3 ']'
+ echo ok
ok
  • 格式3:if …; then … ;elif …; then …; else …; fi
[root@ying01 shell]# vim if03.sh

#!/bin/bash
read -p "请输入考试分数:" a
if [ $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
恭喜你!经过考试,成绩优秀!

6、文件目录属性判断

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

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

[root@ying01 shell]# vim file1.sh

#! /bin/bash
f="/root/ceshi"
if [ -f $f ]
then
        echo $f exist
else
        touch $f
fi

[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/bash
f="/root/ceshi"
if [ -d $f ]
then
        echo $f exist
else
        mkdir $f
        ls -ld $f
fi

[root@ying01 shell]# sh -x file2.sh
+ f=/root/ceshi
+ '[' -d /root/ceshi ']'
+ mkdir /root/ceshi
+ ls -ld /root/ceshi
drwxr-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 $f
fi

或者 ||

f="/root/ceshi"
[ -f $f ] || touch $f    //前面命令不成功时,执行后面的命令

等同于下面的表达方式

if [ ! -f $f ]        //  “!”表示了若是这条命令不成功,就往下执行
then
      touch $f
fi

7、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/bash
n=`wc -l /etc/passwd |awk '{print $1}'`  //输出/etc/passwd的行数
if [ -z "$n" ]                           //条件:是否为空
then
    echo error
    exit
elif [ $n -gt 20 ]                      //条件:n是否大于20
then
    echo OK
fi

[root@ying01 shell]# sh -x  file3.sh
++ wc -l /etc/passwd
++ awk '{print $1}'
+ n=46
+ '[' -z 46 ']'
+ '[' 46 -gt 20 ']'
+ echo OK
OK

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

-n 判断变量的时候,须要用""双引号引发来,如果文件的时候,则不须要用双引号引发来

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

8、case 判断

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

若是case中的某个value是同样的,咱们能够这样写:

在case程序中,能够在条件中使用 |,表示或的意思,  
好比

2|3)     
    command  
    ;;

脚本案例:

#!/bin/bash

read -p "Please input a number: " n     //让用户输入一个数字
if [ -z "$n" ]              //判断用户有没有输入
then
    echo "Please input a number."
    exit 1
fi

n1=`echo $n|sed 's/[0-9]//g'`       //检查用户输入的是否是所有是数字,不是数字就置空
if [ -n "$n1" ]
then
 echo "Please just input a number, without any other words."
 exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]        //通过如上的筛选,咱们来判断输入数字属于哪一个范围,而且把值交给tag
then
    tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
elif [ $n -ge 80 ]  && [ $n -lt 90 ]
then
    tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
    tag=4
else 
    tag=0  //大于100的状况
fi
case $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

9、for循环

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

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

  • for循环
  • while循环

for循环演示:

累加求和

[root@ying01 shell]# vim sum01.sh

#!/bin/bash
sum=0
for i in `seq 1 3`
do
   sum=$[ $sum+$i ]
   echo $i
done
echo SUM=$sum

[root@ying01 shell]# sh -x  sum01.sh 
+ sum=0
++ seq 1 3
+ for i in '`seq 1 3`'
+ sum=1
+ echo 1
1
+ for i in '`seq 1 3`'
+ sum=3
+ echo 2
2
+ for i in '`seq 1 3`'
+ sum=6
+ echo 3
3
+ echo SUM=6
SUM=6

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

[root@ying01 shell]# vim for02.sh

#!/bin/bash
cd /root/100                                //进入到目录
for a in `ls /root/100`                     //遍历此目录
do
   [ -d $a ] && ls $a                       
 # 判断是不是目录,并列出其下文件和子目录
   if [ -d $a ]
   then
       echo $a
       ls $a
   fi
done

[root@ying01 shell]# ls /root/100
10  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 10
10
+ ls 10
\#  4.txt
+ for a in '`ls /root/100`'
+ '[' -d 3 ']'
+ ls 3
2.txt
+ '[' -d 3 ']'
+ echo 3
3
+ ls 3
2.txt

[root@ying01 shell]# sh for02.sh 
\#  4.txt
10
\#  4.txt
2.txt
3
2.txt

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

10、 while循环

语法 while 条件; do … ; done

  • 案例1:

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

[root@ying01 shell]# vim while01.sh

 #!/bin/bash
while :
# 冒号 : 表示死循环的意思,或者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/bash
while :
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  退出循环
done
echo $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: 5
5

11、break跳出循环

break 经常使用于循环语句中,跳出整个循环语句,直接结束全部循环。

[root@ying01 shell]# vim break01.sh

#!/bin/bash
for i in `seq 1 5`
do
    echo A=$i
    if [ $i -eq 3 ]
#比较数字,用-eq ;如果比较的是字符串,那须要用 ==
    then
        break
    fi
    echo B=$i
done
echo C=$i

[root@ying01 shell]# sh break01.sh 
A=1
B=1
A=2
B=2
A=3
C=3

12、continue结束本次循环

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

[root@ying01 shell]# vim contiue01.sh

#!/bin/bash
for i in `seq 1 5`
do
    echo A=$i
    if [ $i -eq 3 ]
    then
        continue
    fi
    echo B=$i
done
echo C=$i

[root@ying01 shell]# sh contiue01.sh  //注意没有B=3行
A=1
B=1
A=2
B=2
A=3
A=4
B=4
A=5
B=5
C=5

十3、exit退出整个脚本

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

[root@ying01 shell]# vim exit01.sh

#!/bin/bash
for i in `seq 1 5`
do
    echo A=$i
    if [ $i -eq 3 ]
    then
        exit
    fi
    echo B=$i
done
echo C=$i

[root@ying01 shell]# sh exit01.sh   //直接从A=3退出;
A=1
B=1
A=2
B=2
A=3
相关文章
相关标签/搜索