20.1 shell介绍

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

结构

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

给脚本添加执行权限“chmod a+x test.sh”,然后直接执行该脚本“./test.sh” bash test.sh;sh test.sh sh参数

-x:sh -x test.sh 查看脚本执行过程 -n:sh -n test.sh 查看脚本是否存在语法错误 eg:

[root@localhost sbin]# vim test.sh #!/bin/bash #This is a example of shell structure #Written by adai

ehco "Hello world" 20.3 date命令用法

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

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

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

显示当前时区的当前时间:

[root@localhost sbin]# date 2017年 09月 03日 星期日 19:36:32 CST 常用日期格式

查看系统日历 [root@localhost sbin]# cal 九月 2017
日 一 二 三 四 五 六 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 注: cal=calendar(日历),“cal -y”可以查看一年的日历。

date +%Y(%y):以四位(两位)数字格式显示年份 [root@localhost sbin]# date +%Y 2017 [root@localhost sbin]# date +%y 17 date "+%Y-%m-%d %H:%M:%S %w" 以上参数分别表示:年、月、日、时、分、秒、星期 [root@localhost sbin]# date "+%Y-%m-%d %H:%M:%S %w" 2017-09-03 19:54:57 0 说明: 如果以上参数组合时中间有特殊符号的话需要加双引号。

date +%F:显示完整的年月日 [root@localhost sbin]# date +%F 2017-09-03 date +%W:显示当前时间是一年的第几周 [root@localhost sbin]# date +%W 35 date +%T:显示当前时间是几点 [root@localhost sbin]# date +%T 18:55:35 date +%s:时间戳,显示从1970年1月1日00:00:00到目前经历的秒数 [root@localhost sbin]# date +%s 1504440220

时间戳换算: [root@localhost sbin]# date +%s -d "2017-09-01 12:00:00" 1504238400 [root@localhost sbin]# date -d @1504238400 2017年 09月 01日 星期五 12:00:00 CST

打印指定日期&时间

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

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

[root@localhost sbin]# date -d "-2 day" +%d 01 [root@localhost sbin]# date -d "-1 year -2 month -1 day" +%Y-%m-%d 2016-07-02 说明: 指定某时间或日期的时候,后面要跟对应的时间格式参数(以上方法同样使用于时分秒)。

时间设置

手动设置时间:date -s “x-x-x x:x:x:” [root@localhost sbin]# date -s "2016-9-3 12:10:00" 2016年 09月 03日 星期六 12:10:00 CST 同步网络时间:ntpdate命令 [root@localhost sbin]# yum install -y ntp #安装ntp的同时会同步安装ntpdate命令

[root@localhost sbin]# ntpdate ntp.ubuntu.com 3 Sep 20:28:54 ntpdate[3407]: step time server 91.189.91.157 offset 31565561.035581 sec

[root@localhost sbin]# date 2017年 09月 03日 星期日 20:29:02 CST 说明: ntpdate后面跟ntp时间服务器地址(国内ntp时间服务器地址:http://www.cnblogs.com/JemBai/archive/2012/04/15/2450045.html )。

20.4 shell脚本中的变量

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

普通变量

[root@localhost sbin]# vim variate.sh #!/bin/bash d=date +%F echo "Today is $d"

[root@localhost sbin]# sh variate.sh Today is 2017-09-03 说明: 该脚本中将变量d定义为了当前日 注意: 在shell脚本中将命令结果定义为变量时要使用反引号,调用变量的方法:“$变量名” 。

内置变量

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

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

[root@localhost sbin]# vim sum.sh #!/bin/bash a=1 b=2 sum=$[$a+$b] echo "$a+$b=$sum"

[root@localhost sbin]# sh sum.sh 1+2=3 注: 数学运算要用[ ]括起来,且前面要加符号$。

和用户交互模式

[root@localhost sbin]# vim sum.sh #!/bin/bash read -p "Please input a number:" x read -p "Please input a number:" y sum=$[$x+$y] echo "$x+$y=$sum"

[root@localhost sbin]# sh sum.sh Please input a number:5 Please input a number:5 5+5=10 注: read命令用于和用户交互,它把用户输入的字符串作为变量值,可以使用-t选项指定读取值时等待的时间(超出时间后自动退出脚本)。

shell脚本预设变量

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

[root@localhost sbin]# vim option.sh #!/bin/bash sum=$[$1+$2] echo "sum=$1+$2=$sum" echo "Result of $0"

[root@localhost sbin]# sh option.sh 3 6 sum=3+6=9 Result of option.sh 说明: 脚本中的$1和$2即为shell的预设变量,分别为脚本的第一个参数和第二个参数,shell脚本预设变量是没有限制的,注意$0位脚本本身的名字。