12.1脚本

  • shell脚本并不能作为正式的变成语言,因为它是在linux的shell中运行的,所以称为shell脚本。shell脚本就是一些命令的集合。把所有的操作都记录到一个文档中,然后去调用文档中的命令,这样一部操作就可以完成。这个文档就是shell脚本,只是这个shell脚本有它的特殊的格式。

  • shell脚本能帮助我们很方便地管理服务器,因为我们可以指定一个任务计划,定时去执行某个shell脚本以满足需求。

凡是自定义的脚本都放到/user/local/sbin/目录下。这样做的目的是:一来可以更好地管理文档;二来是以后接管你的管理员都知道自定义脚本放在哪里,方便维护。

12.1.1 shell脚本的创建和执行

编写第一个shell脚本

[root@localhost ~]# cd /usr/local/sbin
[root@localhost sbin]# vim first.sh
#! /bin/bash

##This is my first shell script
## Writen by Aming 2017-02-23.

date
s is my first shell script
## Writen by Aming 2017-02-23.

date
echo "Hello world"

shell脚本通常都以.sh为后缀名。这并不是说不加.sh的脚本就不能执行,只是大家的一个习惯而已。本例中,脚本文件first.sh的第1行要以#! /bin/bash开头,表示该文件使用的是bash语法。如果不设置改行,你的shell脚本也能执行,但是不符合规范。#表示注释,后面跟一些该脚本的相关注释内容。因为随着工作时间的逐渐过渡,写的shell脚本也会越来越多,如果有一天你回头查看自己写过的某个脚本,很有可能忘记改脚本是用来干什么的以及什么时候写的。所以写上注释是有必要的。别的管理员也可以查看你的脚本。

执行这个脚本,如下所示:

[root@localhost sbin]# sh first.sh
2018年 07月 08日 星期日 20:03:59 CST
Hello world

shell另外的一种执行方法,如下所示:

[root@localhost sbin]# ./first.sh
-bash: ./first.sh: 权限不够
[root@localhost sbin]# chmod +x first.sh
[root@localhost sbin]# ./first.sh
2018年 07月 08日 星期日 20:21:46 CST
Hello world

使用该方法运行shell脚本的前提是脚本本身有执行权限,所以需要给脚本加一个x权限。另外,使用sh命令执行一个shell脚本时,可以加-x选项查看这个脚本执行过程,这样有利于我们调试这个脚本。如下所示:

[root@localhost sbin]# sh -x first.sh
+ date
2018年 07月 08日 星期日 20:25:35 CST
+ echo 'Hello world'
Hello world

date命令

用法:

  • date +%Y:表示意思为数字格式打印年份。

  • date +%y:表示意思为数字格式打印年份。

  • date +%m:表示月份。

  • date +%d:表示日期。

  • date +%H:表示小时。

  • date +%M:表示分钟。

  • date +%S:秒。

  • date +%w:表示星期。结果显示0则表示周日。

列举几个例子date命令的用法,示例如下:

[root@localhost sbin]# date +"%Y-%m-%d %H:%M:%S"

2018-07-08 20:36:12

前一天的日期,如下所示:

[root@localhost sbin]# date -d "-1 day" +%d
07

一小时前,如下所示:

[root@localhost sbin]# date -d "-1 hour" +%H
19

一分钟前,如下所示:

[root@localhost sbin]# date -d "-1 min" +%M
39

12.2 shell脚本中的变量

  • 在shell脚本中使用变量回事我们的脚本更加专业,更像是一门语言。

  • 定义变量的格式为:“变量名=变量的值”。

  • 在脚本中引用变量时需要加上符号$,这跟前面介绍的在shell中自定义变量是一致的。

编写一个与变量相关的脚本

vim variable.sh
#! /bin/bash
## In this script we will use variables.
## Writen by Aming 2017-02-2
d='date +%H:%M:%S'
echo "The script begin at $d."
echo "Now we'll sleep 2 seconds."
sleep 2
d1='date +%H:%M:%S'
echo "The script end at $d1."
sh.variable.sh
The script begin at date +%H:%M:%S.
Now we'll sleep 2 seconds.

数学计算

[root@aminglinux-128 ~]# vim sum.sh

#! /bin/bash
## For get the sum of two numbers.
## Writen by Aming 2017-02-23.
a=1
b=2
sum=$[$a+$b]
echo "$a+$b=$sum"

数学计算要用【】括起来,而且前面要加符号$.

和用户交互

[root@aminglinux-128 ~]# vim read.sh
#! /bin/bash
## Using 'read' in shell script.
## Aming 2017-02-23
read -p  "Please input a number: "x
read -p  "Please input another number: "y
sum=$[$x+$y]
echo "The sum of the two numbers is: $sum"

shell脚本预设变量

shell脚本在执行时,后面可以跟一个或者多个参数。

[root@aminglinux-128 ~]# vim option.sh

#! /bin/bash

sum=$[$1+$2]
echo "sum=$sum"

该脚本的执行结果:

[root@aminglinux-128 ~]# sh -x option.sh 1 2
+ sum=3
+ echo sum=3
sum=3

$0代表脚本本身的名字

#! /bin/bash

sum=$[$1+$2]
echo "$1 $2 $0"

脚本执行的结果

[root@aminglinux-128 ~]# sh option.sh 1 2
1 2 option.sh