shell脚本并不能作为正式的编程语言,因为它是在linux的shell中运行的,所以称为shell脚本。事实上,shell脚本就是一些命令的集合。
假如完成某个需求需要一口气输入10条命令,对于简单的命令,我们可以直接在shell窗口中输入,但如果是比较长且复杂的命令,一次一次敲就会显得很麻烦。我们可以把这10条命令都记录到一个文档中,然后去调用文档中的命令,这样就能一步操作完成,以后再遇到这个需求的时候也只需要调用这个文档就行了,这个文档就是shell脚本,只是这个文档有它特殊的格式。
shell脚本能帮我们很方便地管理服务器,我们可以指定一个任务计划,定时去执行某个shell脚本以满足需求。在正式编写shell脚本之前,建议将所有自定义的脚本放到 /usr/local/sbin/ 目录下,一是可以更好地管理文档,二是方便后面的维护。
下面来编写第一个脚本:
[root@localhost ~]# cd /usr/local/sbin/
[root@localhost sbin]# vim first.sh //写入下面内容
#!/bin/bash
## This is my first shell script.
## Writen by lzx 2018-7-27
date
echo “Hello World!”
shell脚本通常都以.sh作为后缀名,不是说不加.sh的脚本不能运行,只是大家的习惯,这样也方便辨识。
上面,第一行要以#!/bin/bash开头,表示该文件使用的是bash语法,不设置该行也可以执行,但不符合规范。#表示注释,后面跟一些该脚本的相关注释内容,以及作者、创建日期或版本等。注释可以省略,但不建议省略,因为我们的记忆力有限,没有注释我们自己都不知道自己写的脚本是用来干什么的、是什么时候写的。建议从一开始就要养成写脚本的良好习惯,方便自己也方便他人。
下面来执行上面的脚本:
[root@localhost sbin]# sh first.sh //用sh命令来执行
2018年 07月 27日 星期五 09:42:54 CST
Hello World!
还有一种执行方式:
[root@localhost sbin]# ./first.sh //用./来执行,./表示当前目录
-bash: ./first.sh: 权限不够
[root@localhost sbin]# chmod +x first.sh //给执行权限
[root@localhost sbin]# ./first.sh
2018年 07月 27日 星期五 09:44:09 CST
Hello World!
使用上面方法来执行shell脚本的前提是脚本本身有执行权限,所以需要给脚本加一个x权限
另外,使用sh命令来执行脚本时,可以加-x选项来查看脚本的执行过程:
[root@localhost sbin]# sh -x first.sh
+ date
2018年 07月 27日 星期五 09:49:22 CST
+ echo 'Hello World!'
Hello World!
上面有使用date命令,这个命令在shell脚本中使用很频繁,最常见的几个用法如下:
date +%Y :表示以四位数字格式打印年份
date +%y :表示以两位数字格式打印年份
date +%m :表示月份
date +%d :表示日期
date +%H :表示小时
date +%M :表示分钟
date +%S :表示秒
date +%w :表示星期,0~6,0表示星期天
[root@localhost sbin]# date //显示当前时间
2018年 07月 27日 星期五 09:56:08 CST
[root@localhost sbin]# date +"%Y-%m-%d %H:%M:%S" //数字格式显示
2018-07-27 10:02:08
参数说明:
-d datestr : 显示 datestr 中所设定的时间 (非系统时间)
–help : 显示辅助讯息
-s datestr : 将系统时间设为 datestr 中所设定的时间
-u : 显示目前的格林威治时间
–version : 显示版本编号
有时,在脚本中会用到一天前的日期:
[root@localhost sbin]# date -d "-1 day" +%d //-d设定时间,-1 day 表示当前日期前1天,可以以此类推
26
或者一小时前:
[root@localhost sbin]# date -d "-1 hour" +%H //-1 hour表示当前时间前1小时,可以以此类推
09
或者一分钟前:
[root@localhost sbin]# date -d "-1 min" +%M -1 min表示当前时间前1分钟,可以以此类推
09
shell脚本中的变量可以是一个数值、一个命令或者一个路径。定义变量的格式为:“变量名=变量的值”,在脚本中引用变量时需要加上符号$。
下面编写一个与变量有关的脚本:
[root@localhost sbin]# vim variable.sh //写入下面内容
#!/bin/bash
## In this script we will use variables.
## Writen by lzx 2018-7-27
d=date +%H:%M:%S
//反引号的作用是将引号内的字符串当成shell命令执行,返回命令的执行结果
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.”
上面d和d1作为变量出现
执行上面脚本,查看执行结果:
[root@localhost sbin]# sh variable.sh
The script begin at 10:27:22.
Now we'll sleep 2 seconds.
The script end at 10:27:24.
在脚本中进行数字运算,也会用到变量。
下面再自定义一个脚本:
[root@localhost sbin]# vim sum.sh //写入下面内容
#! /bin/bash
## For get the sum of two numbers.
## Writen by lzx 2018-7-27
a=1
b=2
sum= [ [ [a+ b ] e c h o " b] echo " b]echo"a+ b = b= b=sum"
数学计算要用[ ]括起来,并且前面要加上符号$
[root@localhost sbin]# sh sum.sh //执行上面脚本
1+2=3
自定义一个脚本:
[root@localhost sbin]# vim read.sh
#!/bin/bash
## Using 'read' in shell script.
## Writen by lzx 2018-7-27
read -p “Please input a number:” x
read -p “Please input another number:” y
sum= [ [ [x+ y ] e c h o " T h e s u m o f t w o n u m b e r s i s : y] echo "The sum of two numbers is: y]echo"Thesumoftwonumbersis:sum"
read命令用于和用户交互,它把用户输入的字符串作为变量值
[root@localhost sbin]# sh read.sh
Please input a number:5
Please input another number:11
The sum of two numbers is:16
加上 -x选项查看执行过程:
[root@localhost sbin]# sh -x read.sh
+ read -p 'Please input a number:' x
Please input a number:5
+ read -p 'Please input another number:' y
Please input another number:11
+ sum=16
+ echo 'The sum of two numbers is:16'
The sum of two numbers is:16
我们之前有使用过类似 /etc/init.d/iptables restart 这样的命令,前面的 /etc/init.d/iptables 文件其实就是一个shell脚本。那为什么后面可以跟一个restart字符串呢?这就涉及到了shell脚本的预设变量。
实际上,shell脚本在执行时,后面可以跟一个或者多个参数。
下面自定义一个脚本:
[root@localhost sbin]# vim option.sh //写入下面内容
#!/bin/bash
## 测试预设变量
## Writen by lzx 2018-7-27
sum=$[$1+ 2 ] e c h o " s u m = 2] echo "sum= 2]echo"sum=sum"
[root@localhost sbin]# sh option.sh //后面不加参数时,脚本会出错
option.sh:行5: +: 语法错误: 期待操作数 (错误符号是 "+")
[root@localhost sbin]# sh option.sh 5 11 //后面加5和11,5和11就是预设变量$1和$2
sum=16
$1表示脚本的第一个参数,$2表示脚本的第二个参数,以此类推
一个脚本的预设变量数量是没有限制的,另外还有$0,它表示脚本本身的名字
[root@localhost sbin]# vim option1.sh
#!/bin/bash
echo "$1 $2 $0"
[root@localhost sbin]# sh option1.sh 1 2
1 2 option1.sh //可以看到,显示出$0为option1.sh,即脚本名字
如果你学过C语言,那么就不会对if感到陌生。在shell脚本中,我们同样可以使用if逻辑判断。
具体格式如下:
if 判断语句;then
command
fi
下面自定义一个脚本:
[root@localhost sbin]# vim if1.sh //写入下面内容
#!/bin/bash
read -p "Please input your score: " a
if ((a<60));then
echo “You didn’t pass the exam.”
fi
上面出现了((a<60))这样的形式,(( ))这是shell脚本特有的格式,只用一个小括号或不用括号都会报错,要记住这个格式
查看上面脚本的执行结果:
[root@localhost sbin]# sh if1.sh
Please input your score: 90 //没有设置else条件,所以没有结果
[root@localhost sbin]# sh if1.sh
Please input your score: 50
You didn't pass the exam.
具体格式如下:
if 判断语句;then
command
else
command
fi
下面做if1.sh脚本做修改:
[root@localhost sbin]# vim if1.sh //修改为下面内容
#!/bin/bash
read -p "Please input your score: " a
if ((a<60));then
echo “You didn’t pass the exam.”
else
echo “Good!You passed the exam.”
fi
执行上面脚本:
[root@localhost sbin]# sh if1.sh
Please input your score: 90
Good!You passed the exam. //设置了else条件之后,这里返回了结果
[root@localhost sbin]# sh if1.sh
Please input your score: 50
You didn't pass the exam.
具体格式如下:
if 判断语句1;then
command
elif 判断语句2;then
command
else
command
fi
下面自定义一个脚本:
[root@localhost sbin]# vim if2.sh //写入下面内容
#!/bin/bash
read -p "Please input you score: " a
if ((a<60));then
echo “You didn’t pass the exam.”
elif ((a>=60)) && ((a<90));then //elif相对if,再做一次判断
echo “Good!You passed the exam.”
else
echo “Very good!Your score is very high.”
fi
上面的 && 表示“并且”,另外还有 || 表示“或者”
执行上面脚本:
[root@localhost sbin]# sh if2.sh
Please input you score: 40
You didn't pass the exam.
[root@localhost sbin]# sh if2.sh
Please input you score: 85
Good!You passed the exam.
[root@localhost sbin]# sh if2.sh
Please input you score: 95
Very good!Your score is very high.
判断数值大小除了可以使用(( ))的形式外,还可以使用[ ],但不能使用>、<、=这样的符号了,要使用-lt(小于)、-gt(大于)、-le(小于或等于)、-ge(大于或等于)、-eq(等于)、-ne(不等于)
示例如下:
[root@localhost sbin]# a=10; if [ $a -lt 1 ] || [ $a -gt 5 ]; then echo ok; fi //注意空格,否则会有语法错误
ok
[root@localhost sbin]# a=5; if [ $a -lt 10 ] || [ $a -gt 1 ]; then echo ok; fi
ok
shell脚本中if还经常用于判断文档的属性,比如判断是普通文件还是目录,判断文件是否可读、写、执行权限等。
if 常用的选项有以下几个:
-e :判断文件或目录是否存在
-d :判断是不是目录以及是否存在
-f :判断是不是普通文件以及是否存在
-r :判断是否有读权限
-w :判断是否有写权限
-x :判断是否有执行权限
具体格式如下:
if [ -e filename ]; then
command
fi
示例如下:
[root@localhost sbin]# if [ -d /home/ ]; then echo ok; fi
ok
[root@localhost sbin]# if [ -f /home/ ]; then echo ok; fi
/home/是目录而非文件,所以并不会显示ok
[root@localhost ~]# if [ -f /root/test.txt ]; then echo ok; fi
ok
[root@localhost ~]# if [ -r /root/test.txt ]; then echo ok; fi
ok
[root@localhost ~]# if [ -w /root/test.txt ]; then echo ok; fi
ok
[root@localhost ~]# if [ -x /root/test.txt ]; then echo ok; fi
[root@localhost ~]# if [ -e /root/test123.txt ]; then echo ok; fi
在shell脚本中,除了使用if来判断逻辑外,还可以使用case。
具体格式如下:
case 变量 in
value1)
command
;;
value2)
command
;;
value3)
command
;;
*)
command
;;
esac
上面的结构中,不限制value的个数,* 代表其它值。
下面自定义一个脚本:
[root@localhost sbin]# vim case.sh
#!/bin/bash
read -p "Input a number: " n
a=$[n%2]
case $a in
1)
echo “The number is odd.” //odd:奇数
;;
0)
echo “The number is even.” //even:偶数
;;
*)
echo “It’s not a number!”
esac
脚本中的$a的值为1或0,执行结果如下:
[root@localhost sbin]# sh case.sh
Input a number: 100
The number is even.
[root@localhost sbin]# sh case.sh
Input a number: 13
The number is odd.
case脚本常用于编写系统服务的启动脚本
shell脚本算是一门简易的编程语言了,脚本中的循环也是不能缺少的。常用的循环有for循环和while循环。
for循环结构是在日常运维工作中使用很频繁的循环结构。
具体格式如下:
for 变量名 in 循环的条件;do
command
done
下面自定义一个脚本:
[root@localhost sbin]# vim for.sh //写入下面内容
#!/bin/bash
for i in seq 1 5
; do //seq 1 5 表示从1到5的一个序列
echo $i
done
查看执行结果:
[root@localhost sbin]# sh for.sh
1
2
3
4
5
上面**“循环的条件”是引用系统命令的执行结果(如 seq 1 5),但必须用反引号括起来**。
示例如下:
[root@localhost sbin]# for file in `ls`; do echo $file; done
case.sh
check_ng.sh
first.sh
for.sh
if1.sh
if2.sh
option1.sh
option.sh
read.sh
sum.sh
variable.sh
另外“循环的条件”还可以是一组字符或者数字(用一个或多个空格隔开),也可以是一条命令的执行结果。
示例如下:
[root@localhost sbin]# for i in 1 2 3 a b c; do echo $i; done
1
2
3
a
b
c
日常运维工作中,也会经常用while循环来编写死循环的脚本,用于监控某项服务。
具体格式如下:
while 条件; do
command
done
下面自定义一个脚本:
#!/bin/bash
a=6
while [ $a -ge 1]; do
echo a a = a a= aa=[$a-1]
done
查看执行结果:
[root@localhost sbin]# sh while.sh
6
5
4
3
2
1
另外,可以用一个冒号代替循环条件,这样就可以做到死循环。
shell脚本中的函数就是先把一段代码整理到一个小单元中,并给这个小单元命名,当用到这段代码时,直接调用这个小单元的名字即可。
下面自定义一个脚本:
[root@localhost sbin]# vim func.sh
#!/bin/bash
function sum()
{
sum=$[$1+$2]
echo $sum
}
sum $1 $2 //预设变量$1、$2
查看执行结果:
[root@localhost sbin]# sh func.sh 1 2
3
上面func.sh中的sum()为自定义的函数。
在shell脚本中函数的格式如下:
function 函数名()
{
command1
command2
}
需要注意的是,在shell脚本中,函数一定要写在最前面,不能出现在中间或最后。因为函数是要被调用的,如果还没有出现就被调用,那当然会出错。
在shell脚本循环的过程中,我们难免会遇到一些特殊需求,比如当循环到某个地方时需要做一些事情,这时候很有可能需要退出循环,或者跳过本次循环。
break用在循环中,不管是for或者while都可以。在脚本中使用它,表示退出本层循环。之所以说层,是因为有时我们会用到嵌套循环,大循环里面还有小循环,而break仅仅是退出那一层循环,它的上层循环不受影响。
下面自定义一个脚本:
[root@localhost sbin]# vim break.sh
#!/bin/bash
for i in seq 1 5
do
echo $i
if [ $i == 3 ]
then
break
fi
done
echo aaa
查看执行结果:
[root@localhost sbin]# sh break.sh
1
2
3
aaa
上面脚本中,当i=3时,会跳出循环,后面的4和5不再执行,但上层的循环仍在继续。
continue也是使用在循环中的,但和break不同,当在shell脚本中遇到continue时,结束的不是整个循环,而是本次循环(不退出本层循环,只是结束这一次循环)。
下面自定义一个脚本:
[root@localhost sbin]# vim continue.sh
#!/bin/bash
for i in seq 1 5
do
echo $i
if [ $i == 3 ]
then
continue
fi
echo $i
done
查看执行结果:
[root@localhost sbin]# sh continue.sh
1
1
2
2
3
4
4
5
5
当i=3时,因为有continue,结束本次循环,continue后面的语句不再执行,但是后面的循环不受影响
还有一个和break、continue类似的用法,就是exit,它作用的范围更大,直接退出整个shell脚本。
下面修改continue.sh:
[root@localhost sbin]# vim continue.sh //修改为下面内容
#!/bin/bash
for i in seq 1 5
do
echo $i
if [ $i == 3 ]
then
exit
fi
echo $i
done
echo aaa
查看执行结果:
[root@localhost sbin]# sh continue.sh
1
1
2
2
3
这个结果和continue相比,明显exit在i=3时,就直接退出了整个脚本
更多参考资料:
shell基础练习
linux下的shell脚本(基本)