什么是shell脚本
shell脚本并不能作为正式的编程语言,因为它是在linux的shell中运行的,所以称为shell脚本。事实上,shell脚本就是一些命令的集合。
假如完成某个需求需要一口气输入10条命令,对于简单的命令,我们可以直接在shell窗口中输入,但如果是比较长且复杂的命令,一次一次敲就会显得很麻烦。我们可以把这10条命令都记录到一个文档中,然后去调用文档中的命令,这样就能一步操作完成,以后再遇到这个需求的时候也只需要调用这个文档就行了,这个文档就是shell脚本,只是这个文档有它特殊的格式。
shell脚本能帮我们很方便地管理服务器,我们可以指定一个任务计划,定时去执行某个shell脚本以满足需求。在正式编写shell脚本之前,建议将所有自定义的脚本放到 /usr/local/sbin/ 目录下,一是可以更好地管理文档,二是方便后面的维护。
shell脚本的创建和执行
下面来编写第一个脚本:
[root@localhost ~]# cd /usr/local/sbin/
[root@localhost sbin]# vim first.sh //写入下面内容
#! /bin/bash
## This is my first shell script.
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
上面有使用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脚本中的变量
当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替
使用条件语句时,常使用变量 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]
shell中的逻辑判断
格式1:if 条件 ; then 语句; fi
格式2:if 条件; then 语句; else 语句; fi
格式3:if …; then … ;elif …; then …; else …; fi
逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格
可以使用 && || 结合多个条件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then
if 判断文件、目录属性
[ -f file ]判断是否是普通文件,且存在
[ -d file ] 判断是否是目录,且存在
[ -e file ] 判断文件或目录是否存在
[ -r file ] 判断文件是否可读
[ -w file ] 判断文件是否可写
[ -x file ] 判断文件是否可执行
if判断的一些特殊用法
if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
if [ -n "$a" ] 表示当变量a的值不为空
if grep -q '123' 1.txt; then 表示如果1.txt中含有'123'的行时会怎么样
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号
shell中的case判断
格式 case 变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
在case程序中,可以在条件中使用|,表示或的意思, 比如
2|3)
command
;;
案例:
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
案例二
case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number range is 0-100."
;;
esac
for循环
语法:for 变量名 in 条件; do …; done
案例1
#!/bin/bash
sum=0
for i in `seq 1 100`
do
sum=$[$sum+$i]
echo $i
done
echo $sum
文件列表循环
#!/bin/bash
cd /etc/
for a in `ls /etc/`
do
if [ -d $a ]
then
ls -d $a
fi
done
while循环
语法 while 条件; do … ; done
案例1
#!/bin/bash
while :
do
load=`w|head -1|awk -F 'load average: ' '{print $2}'|cut -d. -f1`
if [ $load -gt 10 ]
then
top|mail -s "load is high: $load" asldkfls@11.com
fi
sleep 30
done
案例二:
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ]
then
echo "you need input sth."
continue
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "you just only input numbers."
continue
fi
break
done
echo $n
break跳出循环
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
break
fi
echo $i
done
echo aaaaaaa
continue结束本次循环
忽略continue之下的代码,直接进行下一次循环
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
continue
fi
echo $i
done
echo $i
exit直接退出脚本
#!/bin/bash
for i in `seq 1 5`
do
echo $i
if [ $i == 3 ]
then
exit
fi
echo $i
done
echo aaaaaaa
扩展: