20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量...

第二十章 shell编程

20.1 shell介绍

  • shell是一种脚本语言
  • 可以使用逻辑判断、循环等语法
  • 可自定义函数
  • shell是系统命令的集合
  • shell脚本可以实现自动化运维,能大大增加我们的运维效率

 

20.2 shell脚本结构和执行

结构

  • 开头需要“#!/bin/bash”
  • 脚本内容中以#开头的行作为解释说明
  • 编写脚本时备注:作者、时间、功能等信息,方便之后查看
  • 脚本的名字用“.sh”结尾,用于区分这是一个shell脚本

例子:

[root@cham002 shell]# vim 01.sh 

#!/bin/bash
#written by cham
#2017-09-08
#echo w ls  

echo "123"
w
ls


[root@cham002 shell]# sh 01.sh
123
 22:50:49 up 5 min,  1 user,  load average: 0.02, 0.16, 0.11
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.230.1    22:46    1.00s  0.10s  0.00s w
01.sh

执行方法

  1.  给脚本添加执行权限“chmod a+x 01.sh”,然后直接执行该脚本“./01.sh”
  2.  sh 01.sh     (/bin/bash = .bin/sh = bash)
  3. 绝对路径执行   /root/shell/01.sh     ./ 只是相对路径
  4. [root@cham002 shell]# chmod a+x 01.sh 
    [root@cham002 shell]# ./01.sh 
    123
     22:54:32 up 8 min,  1 user,  load average: 0.07, 0.11, 0.10
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.230.1    22:46    0.00s  0.12s  0.00s /bin/bash ./01.sh
    01.sh
    
    [root@cham002 shell]# ls -l /bin/bash
    -rwxr-xr-x. 1 root root 960392 8月   3 2016 /bin/bash
    [root@cham002 shell]# ls -l /bin/sh
    lrwxrwxrwx. 1 root root 4 10月 19 06:56 /bin/sh -> bash
    
    [root@cham002 shell]# bash 01.sh
    123
     22:57:39 up 11 min,  1 user,  load average: 0.00, 0.06, 0.08
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.230.1    22:46    3.00s  0.13s  0.00s bash 01.sh
    01.sh
    [root@cham002 shell]# sh 01.sh
    123
     22:57:48 up 12 min,  1 user,  load average: 0.00, 0.06, 0.08
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.230.1    22:46    4.00s  0.13s  0.00s w
    01.sh
    

     

sh参数

  • -x:sh -x test.sh 查看显示脚本执行过程
  • -n:sh -n test.sh 查看脚本是否存在语法错误
[root@cham002 shell]# sh -x 01.sh
+ echo 123
123
+ w
 23:09:28 up 23 min,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.230.1    22:46    0.00s  0.42s  0.00s sh -x 01.sh
+ ls
01.sh

[root@cham002 shell]# sh -n 01.sh
#没有任何输出代表没有错

 

20.3 date命令用法

date命令用于显示或设置系统时间与日期。
语法: date [option] 参数

Options:
-d <string>:显示字符串指定的日期与时间(字符串前后必须加上双引号)
-s<string>:根据字符串来设置时间与日期(字符串前后必须加双引号)

参数:
<+时间日期格式>:指定日期和时间显示的格式

[root@cham002 shell]# date
2018年 02月 06日 星期二 23:15:10 CST

常用日期格式 

date +%Y(%y):以四位(两位)数字格式显示年份
[root@cham002 shell]# date +%Y
2018
[root@cham002 shell]# date +%y
18

date "+%Y-%m-%d %H:%M:%S %w"
以上参数分别表示:年、月、日、时、分、秒、星期
[root@cham002 shell]# date "+%Y-%m%d %h:%M:%S %w"
2018-0206 2月:25:50 2


[root@cham002 shell]# date +%m 月
02
[root@cham002 shell]# date +%d 日
06
[root@cham002 shell]# date +%H 时
23
[root@cham002 shell]# date +%M 分
18
[root@cham002 shell]# date +%S 秒
53
[root@cham002 shell]# date +%w星期几
2
[root@cham002 shell]# date +%D 2月6号18年
02/06/18
[root@cham002 shell]# date +%Y%m%d
20180206
[root@cham002 shell]# date +%F
2018-02-06

[root@cham002 shell]# date +%s时间戳
1517930389

[root@cham002 shell]# date +%T
23:20:51
[root@cham002 shell]# date +%H%M%S
232140
[root@cham002 shell]# date +%H:%M:%S
23:21:54
[root@cham002 shell]# date +%W
06

cal=calendar(日历),“cal -y”可以查看一年的日历。
[root@cham002 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

打印指定日期&时间

有时候需要使用N天(小时、分钟、秒(比如昨天的、))前的日期或时间。

格式: date -d "-1 day" +%F

2月6号的昨天
[root@cham002 shell]# date -d "-1 day"
2018年 02月 05日 星期一 23:35:58 CST
[root@cham002 shell]# date -d "-1 day" +%F
2018-02-05

2月份的上个月
[root@cham002 shell]# date -d "-1 month" +%F
2018-01-06

2月6号昨天的时间
[root@cham002 shell]# date -d "-1 hour" +%T
22:41:16
[root@cham002 shell]# date -d "-1 hour" +%T—%F
22:42:12—2018-02-06


说明: 指定某时间或日期的时候,后面要跟对应的时间格式参数(以上方法同样使用于 周、时、分、秒 )。

换算时间戳

[root@cham002 shell]# date +%s
1517931910
[root@cham002 shell]# date -d @1517931910
2018年 02月 06日 星期二 23:45:10 CST
[root@cham002 shell]# date +%s -d "2018-02-06 22:42:12"
1517928132

 

20.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]

[root@cham002 shell]# vim test.sh

#!/bin/bash
d=`date +%F`
echo "today is $d"

[root@cham002 shell]# sh test.sh
today is 2018-02-07

说明: 该脚本中将变量d定义为了当前日
注意: 在shell脚本中将命令结果定义为变量时要使用反引号,调用变量的方法:“$变量名” 。

 

内置变量

$0,$1,$2,$3……

  • $0:表示脚本本身
  • $1:第一个参数
  • $2:第二个参数
  • $#:表示参数的个数

 

[root@cham002 shell]# vim sum.sh

#!/bin/bash
a=6
b=6
sum=$[$a+$b]
echo "$a+$b=$sum"

[root@cham002 shell]# sh sum.sh
6+6=12

注: 数学运算要用[ ]括起来,且前面要加符号$。

 

和用户交互模式

[root@cham002 shell]# vim you.sh

#!/bin/bash
read -p "please input a number:" x
read -p "please input a number:" y
you=$[$x+$y]
echo "$x+$y=$you"

[root@cham002 shell]# sh you.sh
please input a number:8  
please input a number:8
8+8=16
[root@cham002 shell]# sh you.sh
please input a number:7+14
please input a number:7+14
7+14+7+14=42

注: read命令用于和用户交互,它把用户输入的字符串作为变量值,可以使用-t选项指定读取值时等待的时间(超出时间后自动退出脚本)。

shell脚本预设变量

有时候使用类似/etc/init.d/iptables restart的命令,前面的/etc/init.d/iptables文件其实就是一个shell脚本,后面的字符串restart就是预设变量。

[root@cham002 shell]# vim option.sh
#!/bin/bash
you=$[$1+$2]
echo "you=$1+$2=$you"
echo "Result of $0"

[root@cham002 shell]# sh option.sh 36

[root@cham002 shell]# sh option.sh 3 6
you=3+6=
Result of option.sh

[root@cham002 shell]# sh option.sh 7 8
you=7+8=
Result of option.sh

说明: 脚本中的$1和$2即为shell的预设变量,分别为脚本的第一个参数和第二个参数,shell脚本预设变量是没有限制的,注意$0位脚本本身的名字

 

20.5 Shell脚本中的逻辑判断

逻辑表达式

在[ ]中括号中:

  • -lt:=little than 小于  <
  • -le:=little && equal 小于等于  <=
  • -eq:=equal 等于  ==
  • -ne:=no equal 不等于  !=
  • -gt:=greater than 大于  >
  • -ge:=greater && equal 大于等于  >=

可以用用符号,但要在(( ))小括号中:类似: if (($a>1)); then echo ok;fi 

ok

<,<=,==,!=,>,>=

注意: 使用双括号括起来

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

[root@cham002 shell]# vim if1.sh

#!/bin/bash
a=5
 if [ $a -gt 3 ]  #注意[]内的空格
 then
      echo ok
 fi

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

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

[root@cham002 shell]# vim if2.sh 

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

[root@cham002 shell]# sh -x if2.sh 
+ a=1
+ '[' 1 -gt 3 ']'
+ echo nook
nook

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

[root@cham002 shell]# vim if3.sh

#!/bin/bash
a=5
if [ $a -lt 3 ]
then
      echo "a<3"
elif [ $a -gt 6 ]
then
      echo "a>6"
else
      echo nook
 fi
~       

[root@cham002 shell]# sh -x if3.sh
+ a=5
+ '[' 5 -lt 3 ']'
+ '[' 5 -gt 6 ']'
+ echo nook
nook

关系

各个条件之间的关系可以使用逻辑连接符:

条件A&&条件B:并且
条件A||条件B:或者

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

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

 

20.6 文件目录属性判断

shell脚本中if经常用于判断文档的属性,比如判断是普通文件还是目录文件,判断文件是否有读、写、执行权限等。if常用的选项有以下几个:

  • -e:判断文件或目录是否存在
  • -d:判断是不是目录文件以及是否存在
  • -f:判断是不是普通文件以及是否存在
  • -r:判断是否有读权限
  • -w:判断是否有写权限
  • -x:判断是否有执行权限

格式 

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

[root@cham002 shell]# vim file1.sh

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


[root@cham002 shell]# sh -x file1.sh
+ f=/tmp/chamlinux
+ '[' -f /tmp/chamlinux ']'  逻辑判断文件不存在
+ touch /tmp/chamlinux       不存在就会创建

再次执行
[root@cham002 shell]# sh -x file1.sh
+ f=/tmp/chamlinux
+ '[' -f /tmp/chamlinux ']'  逻辑判断文件存在
+ echo /tmp/chamlinux exist
/tmp/chamlinux exist

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

[root@cham002 shell]# cp file1.sh file2.sh
[root@cham002 shell]# vi !$
vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -d $f ]
then
    echo $d exist
else
    touch $f
fi

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

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

[root@cham002 shell]# vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -e $f ]
then
    echo $d exist
else
    touch $f
fi
       

[root@cham002 shell]# sh -x file2.sh 
+ f=/tmp/chamlinux
+ '[' -e /tmp/chamlinux ']'
+ echo exist
exist

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

[root@cham002 shell]# vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -r $f ]
then
    echo $f readable
fi

[root@cham002 shell]# sh -x file2.sh 
+ f=/tmp/chamlinux
+ '[' -r /tmp/chamlinux ']'
+ echo /tmp/chamlinux readable
/tmp/chamlinux readable

[root@cham002 shell]# sh  file2.sh 
/tmp/chamlinux readable

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

[root@cham002 shell]# vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -w $f ]
then
    echo $f writeable
fi

[root@cham002 shell]# sh  file2.sh 
/tmp/chamlinux writeable

[root@cham002 shell]# sh -x file2.sh 
+ f=/tmp/chamlinux
+ '[' -w /tmp/chamlinux ']'
+ echo /tmp/chamlinux writeable
/tmp/chamlinux writeable

#对root用户来讲是可读可写的,如果是其他用户就可能是只读。
[root@cham002 shell]# ls -l /tmp/chamlinux
-rw-r--r-- 1 root root 0 2月   7 01:08 /tmp/chamlinux

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

[root@cham002 shell]# vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
if [ -x $f ]
then
    echo $f exeable
fi
       

[root@cham002 shell]# sh  file2.sh   #因为不可执行所以没有输出,没有定义else
[root@cham002 shell]# sh -x file2.sh 
+ f=/tmp/chamlinux
+ '[' -x /tmp/chamlinux ']'

 另外一种用法,判断这个文件或者目录存不存在,如果存在就删除,“&&”表示当前面的命令执行成功的时候才会执行后面的命令 。“||”表示文件不存在才会执行后面的操作

[root@cham002 shell]# vi file2.sh

#!/bin/bash
f="/tmp/chamlinux"
[ -f /tmp/chamlinux ] && rm -f $f


如果文件不存在
#!/bin/bash
f="/tmp/chamlinux"
if [ ! -f $f ]
then 
    touch $f
fi

 

20.7 if 特殊用法

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

[root@cham002 shell]# vim if4.sh

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


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


[root@cham002 shell]# vim if4.sh

#!/bin/bash
if [ ! -f /tmp/lalal ]     
then
    echo "/tmp/lalal not exist."   即,如果变量n为空则显示error并退出该脚本。  
    exit
fi
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
     echo error
     exit
elif [ $n -gt 100 ]
then
     echo fdsfsdfdf
fi


[root@cham002 shell]# sh -x if4.sh
+ '[' '!' -f /tmp/lalal ']'
+ echo '/tmp/lalal not exist.'
/tmp/lalal not exist.
+ exit
[root@cham002 shell]# sh  if4.sh
/tmp/lalal not exist.

即,当该文件不存在的时候就会退出执行,不会提示存在语法错误。(该脚本存在逻辑错误,只做效果演示用)

注意: 在该表达式中引用变量时要用双引号引起来。

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

[root@cham002 shell]# ls
01.sh  file1.sh  file2.sh  if1.sh  if2.sh  if3.sh  if4.sh  option.sh  sum.sh  test.sh  you.sh
[root@cham002 shell]# if [ -n 01.sh ]; then echo ok; fi
ok

[root@cham002 shell]# if [ -n 01.sh ]; then echo ok; fi
ok
[root@cham002 shell]# if [ -n "$b" ]; then echo $b; else echo "b is mull";fi
b is mull

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

[root@cham002 shell]# if grep -w 'user1' /etc/passwd; then echo "user1 exist"; fi
user1:x:1000:1000::/home/user1:/bin/bash
user1 exist
[root@cham002 shell]# if grep -wq 'user1' /etc/passwd; then echo "user1 exist"; fi
user1 exist

判断某参数存在:
[root@cham002 shell]# vim if123.sh
#!/bin/bash
if
	grep -wq 'user1' /etc/passwd
then
	echo "user1 exist."
fi

[root@cham002 shell]# sh if123.sh
user1 exist.
[root@cham002 shell]# sh -x if123.sh
+ grep -wq user1 /etc/passwd
+ echo 'user1 exist.'
user1 exist.

判断某参数不存在:
[root@cham002 shell]# vim ifno123.sh
#!/bin/bash
if 
        ! grep -wq 'user10' /etc/passwd
then
        echo "no user1"
fi

[root@cham002 shell]# sh ifno123.sh
no user1


#!/bin/bash
if 
        ! grep -wq 'user10' /etc/passwd
then
        useradd user10
        echo "yes"
fi

[root@cham002 shell]# sh ifno123.sh
useradd:警告:此主目录已经存在。
不从 skel 目录里向其中复制任何文件。
正在创建信箱文件: 文件已存在
yes

[root@cham002 shell]# sh -x ifno123.sh
+ grep -wq user10 /etc/passwd
+ useradd user10
useradd:警告:此主目录已经存在。
不从 skel 目录里向其中复制任何文件。
正在创建信箱文件: 文件已存在
+ echo yes
yes

说明: grep中-w选项=Word,表示过滤一个单词;-q,表示不打印过滤的结果。判断某参数不存在时使用!表示取反。

 

20.8-20.9 case判断

格式:

case 变量名 in
    value1)
       commond1
       ;;
    value2)
       commod2
       ;;
    value3)
       commod3
       ;;
esac

在case中,可以在条件中使用“|”,表示或的意思,如:

2|3)
    commond
    ;;

太晚了,记得case还没做!!!!!!!!!!!!!!!!!!!!!

转载于:https://my.oschina.net/u/3708120/blog/1619371

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值