• 目录
  • 1.Shell脚本介绍
  • 2.Shell脚本结构和执行
  • 3.date命令用法
  • 4.shell脚本中的变量
  • 5.Shell脚本中的逻辑判断
  • 6.文件目录属性判断
  • 7.if特殊用法
  • 8.case判断
  • 9.for循环
  • 10.while循环
  • 11.break跳出循环
  • 12.continue结束本次循环
  • 13.exit退出整个脚本
  • 扩展

 

 

1.Shell脚本介绍

shell是一种脚本语言    blog.lishiming.net

可以使用逻辑判断、循环等语法

可以自定义函数

shell是系统命令的集合

shell脚本可以实现自动化运维,能大大增加我们的运维效率

 

2.Shell脚本结构和执行

开头需要加#!/bin/bash(接下来的命令是通过哪个解释器来执行)

以#开头的行作为解释说明

脚本的名字以.sh结尾,用于区分这是一个shell脚本

执行方法有两种

chmod +x 1.sh; ./1.sh

bash 1.sh

查看脚本执行过程 bash -x 1.sh

查看脚本是否语法错误  bash -n 1.sh

[root@chenshi shell]# bash -n 1.sh 
1.sh:行6: 语法错误: 未预期的文件结尾

 

3.date命令用法

[root@chenshi shell]# date
2018年 07月 27日 星期五 11:16:15 CST

date标记年、月、日

[root@chenshi shell]# date +%Y
2018
[root@chenshi shell]# date +%y
18
[root@chenshi shell]# date +%m月
07
[root@chenshi shell]# date +%M分钟
19
[root@chenshi shell]# date +%d日
27
[root@chenshi shell]# date +%D
07/27/18

[root@chenshi shell]# date +%Y%m%d
20180727
[root@chenshi shell]# date +%F
2018-07-27

时间标记

[root@chenshi shell]# date +%F
2018-07-27
[root@chenshi shell]# date +%H  几点
11
[root@chenshi shell]# date +%s  时间戳(距离1970年1月1日0点0分到现在的时间)
1532661683
[root@chenshi shell]# date +%S  秒
29

[root@chenshi shell]# date +%T
11:22:42
[root@chenshi shell]# date +%H:%M:%S
11:23:41


[root@chenshi shell]# date +%h
7月

[root@chenshi shell]# date +%w  周几
5

[root@chenshi shell]# date +%W 今年的第几周
30

日历

[root@chenshi 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
date -d "+1day"    一天后
 date -d "-1 day"   一天前
 date -d "-1 month" 一月前
 date -d "-1 min"   一分钟前
 date +%w, date +%W 星期
[root@chenshi shell]# date -d "-1 day" +%F
2018-07-26
[root@chenshi shell]# date -d "+1 day" +%F
2018-07-28

4.shell脚本中的变量

脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替

使用条件语句时,常使用变量    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脚本中的逻辑判断

格式1:if 条件 ; then 语句; fi

[root@chenshi shell]# if [ $a -gt 3 ] 
> then
> echo ok
> fi
ok

#!/bin/bash
a=5
if [ $a -gt 3 ]
then
    echo ok
fi
~        

[root@chenshi shell]# sh if1.sh 
ok

格式2:if 条件; then 语句; else 语句; fi

#!/bin/bash
a=3
if [ $a -gt 3 ]
then
    echo ok
else
    echo not ok
fi

[root@chenshi shell]# sh -x if2.sh 
+ a=3
+ '[' 3 -gt 3 ']'
+ echo not ok
not ok

格式3:if …; then … ;elif …; then …; else …; fi

#!/bin/bash
a=3
if [ $a -gt 4 ]
then
    echo ">1"
elif [ $a -lt 6 ]
then 
    echo "<6 && >1"
else
    echo not ok
fi

[root@chenshi shell]# sh -x if3.sh 
+ a=3
+ '[' 3 -gt 4 ']'
+ '[' 3 -lt 6 ']'
+ echo '<6 && >1'
<6 && >1

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

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

if [ $a -gt 5 ] && [ $a -lt 10 ]; then 并且

if [ $b -gt 5 ] || [ $b -lt 3 ]; then 或者

 

6.文件目录属性判断

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

[root@chenshi shell]# cat file1.sh 
#!/bin/bash
f="/tmp/xiaoqi"
if [ -f $f ]
then
    echo $f exist
else
   touch $f
fi
[root@chenshi shell]# sh -x file1.sh 
+ f=/tmp/xiaoqi
+ '[' -f /tmp/xiaoqi ']'
+ touch /tmp/xiaoqi

[root@chenshi shell]# sh -x file1.sh 
+ f=/tmp/xiaoqi
+ '[' -f /tmp/xiaoqi ']'
+ echo /tmp/xiaoqi exist
/tmp/xiaoqi exist

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

[root@chenshi shell]# cat file2.sh 
#!/bin/bash
f="/tmp/xiaoqi"
if [ -d $f ]
then
    echo $f exist
else
   touch $f
fi

[root@chenshi shell]# sh -x file2.sh 
+ f=/tmp/xiaoqi
+ '[' -d /tmp/xiaoqi ']'
+ touch /tmp/xiaoqi

[ -e file ] 判断文件或目录是否存在

[ -r file ] 判断文件是否可读

[root@chenshi shell]# cat file2.sh 
#!/bin/bash
f="/tmp/xiaoqi"
if [ -r $f ]
then
    echo $f read
fi

[root@chenshi shell]# sh -x file2.sh 
+ f=/tmp/xiaoqi
+ '[' -r /tmp/xiaoqi ']'
+ echo /tmp/xiaoqi read
/tmp/xiaoqi read

[ -w file ] 判断文件是否可写

[root@chenshi shell]# cat file2.sh 
#!/bin/bash
f="/tmp/xiaoqi"
if [ -w $f ]
then
    echo $f writeable
fi

[root@chenshi shell]# sh file2.sh 
/tmp/xiaoqi writeable

[ -x file ] 判断文件是否可执行

[root@chenshi shell]# cat file2.sh 
#!/bin/bash
f="/tmp/xiaoqi"
if [ -x $f ]
then
    echo $f exeable
fi

[root@chenshi shell]# sh file2.sh 
[root@chenshi shell]# sh -x file2.sh 
+ f=/tmp/xiaoqi
+ '[' -x /tmp/xiaoqi ']'

加上 ! 表示取反。

 

7.if特殊用法

if [ -z "$a" ]  这个表示当变量a的值为空时会怎么样 =[ ! -n ]

#!/bin/bash
n=`wc -l /tmp/lalal`
fi [ -z "$n" ]
then
   echo error
else
   if [ $n -gt 100 ]
   then
       echo asdasdasd
   fi
fi

[root@chenshi shell]# sh -x if4.sh 
++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ '[' -z '' ']'
+ echo error
error

if [ -n "$a" ] 表示当变量a的值不为空

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

if grep -q '123' 1.txt; then  表示如果1.txt中含有'123'的行时会怎么样

[root@chenshi shell]# if grep -wq 'root' /etc/passwd; then echo "user1 exist"; fi
user1 exist

if [ ! -e file ]; then 表示文件不存在时会怎么样

if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…

[ ] 中不能使用<,>,==,!=,>=,<=这样的符号

[root@chenshi shell]# cat if4.sh 
#!/bin/bash
n=`wc -l /tmp/lalal`
if [ $n -gt 100 ]
then 
    echo asdasdasd
fi

当文件不存在时脚本就会报错


[root@chenshi shell]# sh -x if4.sh 
++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ '[' -gt 100 ']'
if4.sh: 第 3 行:[: -gt: 期待一元表达式

 

8.case判断

格式 case  变量名 in 
                     value1)         
                          command
                          ;;                                   表示此判断结束,进入下个判断
                     value2)
                          command
                          ;;
                      *)
                        commond
                            ;;
                      esac

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

脚本案例

[root@chenshi shell]# vi case.sh

 #!/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 input a number."
 exit 1
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]
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
fi
case $tag in
    1)
	echo "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;; 
esac

测试脚本

[root@chenshi shell]# sh case.sh 
Please input a number: 50
not ok
[root@chenshi shell]# sh case.sh 
Please input a number: 90
oook
[root@chenshi shell]# sh case.sh 
Please input a number: 100
oook
[root@chenshi shell]# sh case.sh 
Please input a number: 60
ok

[root@chenshi shell]# sh -x case.sh 
+ read -p 'Please input a number: ' n
Please input a number: 60
+ '[' -z 60 ']'
++ echo 60
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ '[' 60 -lt 60 ']'
+ '[' 60 -ge 60 ']'
+ '[' 60 -lt 80 ']'
+ tag=2
+ case $tag in
+ echo ok
ok

 

9.for循环

语法:for 变量名 in 条件; do …; done

编写脚本

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

执行脚本

[root@chenshi shell]# sh for1.sh 
5050
[root@chenshi shell]# sh -x for1.sh 
+ sum=0
++ seq 1 100
+ for i in '`seq 1 100`'
+ sum=1
+ for i in '`seq 1 100`'
+ sum=3
+ for i in '`seq 1 100`'
.
.
+ for i in '`seq 1 100`'
+ sum=5050
+ echo 5050
5050

案例2,文件列表循环

#!/bin/bash
cd /etc/
for a in `ls /etc/`
do 
     [ -d $a ] && ls $a
done

for循环中当对象有空格时容易出错。

 

10.while循环

语法 while 条件; do … ; done

编写脚本

[root@chenshi shell]# vi while1.sh

#!/bin/bash
while :
do
    load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
    if [ $load -gt 10 ]
    then
        top|mail -s "load is high: $load" 
        fi
    sleep 30
done

[root@chenshi shell]# w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1
2

脚本2

#!/bin/bash
while :
do
    read -p "Please input a number: " n
    if [ -z "$n" ]
    then
        echo "you need input sth."
        continue                               继续循环
    fi
    n1=`echo $n|sed 's/[0-9]//g'`
    if [ -n "$n1" ]
    then
        echo "you just only input numbers."
        continue
    fi
    break
done
echo $n

查看脚本运行状况

[root@chenshi shell]# sh while2.sh 
Please input a number: j
you just only input numbers.
Please input a number: 1
1

[root@chenshi shell]# sh while2.sh 
Please input a number: 
you need input sth.

 

11.break跳出循环

编写脚本

#!/bin/bash
for i in `seq 1 5`
do 
     echo $i
     if [ $i -eq 3 ]
     then
          break
     fi 
     echo $i
done
echo aaaa
~

执行脚本

[root@chenshi shell]# sh -x break.sh 
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 -eq 3 ']'
+ echo 1
1
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 -eq 3 ']'
+ echo 2
2
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 -eq 3 ']'
+ break
+ echo aaaa
aaaa

 

12.continue结束本次循环

编写脚本

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

执行脚本

[root@chenshi shell]# sh break.sh 
1
1
2
2
3
4
4
5
5

 

13.exit退出整个脚本

编写脚本

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

执行脚本

[root@chenshi shell]# sh break.sh 
1
1
2
2
3

 

扩展
select用法 http://www.apelearn.com/bbs/thread-7950-1-1.html