十六周五次课

十六周五次课

20.1shell脚本介绍:

20.2shell脚本结构和执行方法

20.3date命令用法

20.4shell脚本中的变量

20.1shell脚本介绍:

摘要: Shell是一个用C 语言编写的程序, 它是用户操作Linux的桥梁, 也就是平时我们连接Linux 的所看到的命令行窗口。平时我们输入的Linux 命令都是在Shell 程序中完成的。Shell 脚本, 我认为就是一组在Shell 程序中按照顺序执行的命令。通常使用的shell不会是太复杂的操作, 因为shell 脚本对于实现一些比较复杂的逻辑比较麻烦,因为对于比较复杂的逻辑可以使用 python 语言实现。

shell是一种脚本语言

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

可自定义函数

shell是系统命令的集合

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

shell就是系统中敲过的一些命令把它们写到了文件中,然后再去执行这个文件,批量的执行文件里的命令。

要想学好shell,首先要熟悉命令,各种服务的搭建,包括各种服务的管理和配置,只有懂了这些,才能写出来。另外,除了以上这些,还要懂一些判断。

20.2shell脚本结构和执行方法

结构

开头需要“#!/bin/bash”

脚本内容中以#开头的行作为解释说明

编写脚本时备注:作者、时间、功能等信息,方便之后查看

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

[root@localhost ~]# mkdir shell
[root@localhost ~]# cd shell
[root@localhost shell]# ls
[root@localhost shell]# vim 01.sh

#!/bin/bash
echo "123"
w
ls

[root@localhost shell]# sh 01.sh 
123
 07:47:04 up 30 min,  1 user,  load average: 0.04, 0.04, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.231.1    07:44    0.00s  0.17s  0.00s sh 01.sh
01.sh
[root@localhost shell]# 

第1行以#!/bin/bash开头,表示该文件使用的是bash语法。如果不设置该行,你的shell脚本也可以执行,但是不符合规范。

[root@localhost shell]# vim 01.sh 


echo "123"
w
ls

保存,退出。

[root@localhost shell]# sh 01.sh 
123
 07:51:14 up 34 min,  1 user,  load average: 0.02, 0.03, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.231.1    07:44    2.00s  0.20s  0.00s sh 01.sh
01.sh
[root@localhost shell]# 

可以运行,说明这台机器是可以识别这些一条一条的命令的。如果不加开头,换一台机器,可能不会执行,开头是为了说明使用哪个解释器(/bin/bash)来执行的。

执行方法:

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

bash test.sh;sh test.sh

[root@localhost shell]# chmod a+x 01.sh 
[root@localhost shell]# ./01.sh 

123
 07:55:47 up 39 min,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.231.1    07:44    3.00s  0.22s  0.00s /bin/bash ./01.sh
01.sh
[root@localhost shell]# 

既然能够执行,就说明被解析了,被/bin/bash认识了。

[root@localhost shell]# cat 01.sh 
#!/bin/bash
echo "123"
w
ls
[root@localhost shell]# 

其实,/bin/bash是一条命令。

[root@localhost shell]# ll /bin/bash
-rwxr-xr-x. 1 root root 960472 Aug  3 05:11 /bin/bash
[root@localhost shell]# 

[root@localhost shell]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 Dec 21 11:43 /bin/sh -> bash
[root@localhost shell]# 

/bin/bash和/bin/sh是同一个文件,如果没有开头#!/bin/bash,可以这样执行。

[root@localhost shell]# /bin/bash 01.sh 
123
 08:00:29 up 44 min,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.231.1    07:44    5.00s  0.26s  0.00s /bin/bash 01.sh
01.sh
[root@localhost shell]# 

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

[root@localhost shell]# sh -x 01.sh 
+ echo 123
123
+ w
 07:22:14 up 11:39,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.231.1    07:21    6.00s  0.07s  0.01s w
root     pts/1    192.168.231.1    20:36    8:31m  0.05s  0.05s -bash
+ ls
01.sh
[root@localhost shell]# 

每一个+都是一个命令,下面一行是该命令的执行结果。

sh -n test.sh 查看脚本是否存在语法错误,如果出现语法问题,会报错,但报错的地方可能不太精确。

[root@localhost shell]# sh -n 01.sh 
[root@localhost shell]# 

20.3date命令用法

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

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

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

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

[root@localhost shell]# date
Wed Jan 24 07:30:01 CST 2018
[root@localhost shell]# 

常用日期格式

查看系统日历

[root@localhost shell]# cal
    January 2018    
Su Mo Tu We Th Fr Sa
    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@localhost shell]# 

注: cal=calendar(日历),“cal -y”可以查看一年的日历。

date +%Y(%y):以四位(两位)数字格式显示年份

[root@localhost shell]# date +%Y
2018
[root@localhost shell]# date +%y
18
[root@localhost shell]# 

date "+%Y-%m-%d %H:%M:%S %w"
以上参数分别表示:年、月、日、时、分、秒、星期

[root@localhost shell]# date "+%Y-%m-%d %H:%M:%S %w"
2018-01-24 07:36:27 3
[root@localhost shell]# 

date +%F:显示完整的年月日

[root@localhost shell]# date +%F
2018-01-24

date +%W:显示当前时间是一年的第几周

[root@localhost shell]# date +%W
04

date +%T:显示当前时间是几点

[root@localhost shell]# date +%T
07:39:43

date +%s:时间戳,显示从1970年1月1日00:00:00到目前经历的秒数
[root@localhost shell]# date +%s

1516750791

[root@localhost shell]#  date +%s -d "2017-01-24 07:42:30"
1485214950
[root@localhost shell]# date -d @1485214950
Tue Jan 24 07:42:30 CST 2017
[root@localhost shell]# 

打印制定日期&时间

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

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

[root@localhost shell]# date -d "-2 day" +%d
22
[root@localhost shell]# date -d "-1 year -2 month -1 day" +%Y-%m-%d
2016-11-23

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

时间设置

手动设置时间:date -s “x-x-x x:x:x:”

[root@localhost shell]# date -s "2018-01-24 07:46:30"
Wed Jan 24 07:46:30 CST 2018

同步网络时间:ntpdate命令

[root@localhost shell]# yum install -y ntp

#安装ntp的同时会同步安装ntpdate命令

[root@localhost shell]# ntpdate ntp.ubuntu.com
24 Jan 07:48:15 ntpdate[12847]: step time server 91.189.89.198 offset -18.244893 sec

[root@localhost shell]# date
Wed Jan 24 07:48:43 CST 2018

20.4shell脚本中的变量

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

普通变量

[root@tianqi-01 shell]# vim variate.sh 

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

[root@tianqi-01 shell]# sh variate.sh 
Today is 2018-01-23
[root@tianqi-01 shell]# 

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

内置变量

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

$0:表示脚本本身

$1:第一个参数

$2:第二个参数

$#:表示参数的个数

数学运算

[root@tianqi-01 shell]# vim sum.sh

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

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

和用户交互模式

[root@tianqi-01 shell]# 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@tianqi-01 shell]# sh sum.sh 
Please input a number:6
Please input a number:7
6+7=13
[root@tianqi-01 shell]# 

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

shell脚本预设变量

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

[root@tianqi-01 shell]# vim option.sh 

#!/bin/bash
sum=$[$1+$2]
echo "sum=$1+$2=$sum"
echo "Result of $0"

[root@tianqi-01 shell]# sh option.sh 7 8
sum=7+8=15
Result of option.sh
[root@tianqi-01 shell]# 

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

转载于:https://my.oschina.net/u/3744518/blog/1612133

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值