shell的简单概念
shell是一种脚本语言,是用户界面和系统底层之间的通信桥梁,是系统命令的集合,支持一些基本的编程元素,例如:
- if…else 选择结构,switch…case 开关语句,for、while、until 循环;
- 变量、数组、字符串、注释、加减乘除、逻辑运算等概念;
- 函数,包括用户自定义的函数和内置函数(例如 printf、export、eval 等)。
shell脚本可以实现自动化运维,能大成都的增加人工运维效率
shell脚本结构和执行
以之前监控nginx状态的脚本为例
[root@linux2019_2 ~]# ] vi /usr/local/sbin/nginx_status.sh
#!/bin/bash
url="http://127.0.0.1/nginx_status"
curl=/usr/bin/curl
# 检测nginx进程是否存在
function ping {
/sbin/pidof nginx | wc -l
}
# 检测nginx性能
function active {
$curl $url 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
$curl $url 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
$curl $url 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
$curl $url 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
$curl $url 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
$curl $url 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
$curl $url 2>/dev/null| awk NR==3 | awk '{print $3}'
}
$1
- 开头必须要写明#!/bin/bash—指定脚本使用的shell
- 以#开头的行作为解释说明该脚本的用途、作者等相关信息(可选)
- 主要环境变量的声明(可选)
- 脚本程序部分
- 脚本的名字以.sh结尾,用于区分这是一个shell脚本
- 执行方法有以下两种:
chmod +x nginx_status.sh ; ./nginx_status.sh
bash nginx_status.sh
sh nginx_status.sh
- 查看脚本执行过程 #bash -x nginx_status.sh
- ==查看脚本是否语法错误 #bash -n nginx_status.sh ==
date命令的基本用法
#date +%Y-%m-%d ; date +%y-%m-%d 年月日
[root@linux2019_3 ~]# date +%Y-%m-%d ; date +%y-%m-%d
2019-03-06
19-03-06
#date +%H:%M:%S = date +%T 时间
[root@linux2019_3 ~]# date +%H:%M:%S
11:59:51
[root@linux2019_3 ~]# date +%T
11:59:57
#date +%s 时间戳
[root@linux2019_3 ~]# date +%s
1551844832
#date -d @1551844944
[root@linux2019_3 ~]# date -d @1551844944
2019年 03月 06日 星期三 12:02:24 CST
#date -d "+1day" 一天后
[root@linux2019_3 ~]# date -d "+1day"
2019年 03月 07日 星期四 12:03:26 CST
#date -d "-1 day" 一天前
[root@linux2019_3 ~]# date -d "-1day"
2019年 03月 05日 星期二 12:03:48 CST
#date -d "-1 month" 一月前
[root@linux2019_3 ~]# date -d "+1month"
2019年 04月 06日 星期六 12:04:03 CST
#date -d "-1 min" 一分钟前
[root@linux2019_3 ~]# date -d "-min"
2019年 03月 06日 星期三 12:05:30 CST
#date +%w, date +%W 星期
[root@linux2019_3 ~]# date +%W
09 #今年的第9周
[root@linux2019_3 ~]# date +%w
3 #本周周三
shell脚本中的变量
当脚本中使用某个字符串较频繁并且字符串长度很长时就可以使用变量代替
- 变量名可以由字母、数字和下画线组成,但是不能以数字开头
- 变量用等号"="连接值,"="左右两侧不能有空格
- 变量值中如果有空格,则需要使用单引号或双引号包含,例如a=“hello world”
- 在变量值中,可以使用转义符""来转义特殊字符本身的含义
- 如果要把命令的执行结果作为变量值赋予变量,则需要使用反引号或 $() 包含命令
- 环境变量名建议大写,便于区分
- 内置变量 $0, $1, $2… $0表示脚本本身,$1 第一个参数,$2 第二个参数,以此类推, $#表示参数个数
- 在写需要和用户交互的脚本时,变量也是必不可少的
read -p "Input a number: " n; echo REPLY - 在 Bash 中,变量的默认类型都是字符串型,如果要进行数值运算,则必须指定变量类型为数值型
[root@linux2019_3 ~]# a=1+2;echo $a
1+2
[root@linux2019_3 ~]# a=1;b=2; c=$(($a+$b)); echo $c
3
[root@linux2019_3 ~]# a=1;b=2;c=$[$a+$b] ;echo $c
3
# c=$(($a+$b))等效于$[$a+$b]
shell中的逻辑判断
- 格式1:if 条件 ; then 语句; fi
- 格式2:if 条件; then 语句; else 语句; fi
- 格式3:if …; then … ;elif …; then …; else …; fi
-eq—等于
-ne—不等于
-gt—大于
-lt—小于
-ge—大于等于
-le—小于等于
&&—逻辑与
||—逻辑或
!—逻辑非
if 判断文件、目录属性
- [ -f file ] 判断是否是普通文件,且存在
- [ -d file ] 判断是否是目录,且存在
- [ -e file ] 判断文件或目录是否存在
- [ -r file ] 判断文件是否可读
- [ -w file ] 判断文件是否可写
- [ -x file ] 判断文件是否可执行
[root@linux2019 ~]# ll abc
ls: 无法访问abc: 没有那个文件或目录
[root@linux2019 ~]# if ! ls abc &>/dev/null ;then echo zhilin is wrong; fi
zhilin is wrong
# !取反
# &>/dev/null 不管结果如何,丢弃结果
root用户对文件的读写比较特殊,即使一个文件没有给root用户读或者写的权限,root用户照样可以读或者写,但是执行的权限是正常的
if判断的一些特殊用法
- if [ -z “$a” ] 这个表示判断变量a的值为空时满足条件
- if [ -n “$a” ] 表示判断变量a的值不为空时满足条件
- if grep -q ‘123’ 1.txt; then 表示判断1.txt中含有’123’的行时满足条件
[root@linux2019 ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@linux2019 ~]# grep -q root /etc/passwd ;echo $?
0
[root@linux2019 ~]# grep -q roottt /etc/passwd ;echo $?
1
- if (($a<1)); then …等同于 if [ $a -lt 1 ]; then… ,[ ] 中不能使用<,>,,!=,>=,<=这样的符号==