一、Shell简介
一)什么是shell脚本
shell是linux的一种外壳,它在linux内核的外面,为用户和内核之间的交互提供了一个接口
当用户下达指令给操作系统的时候,实际上是把指令告诉shell,经过shell解释,处理后让内核作出相应的动作
系统的回应和输出的信息也由shell处理,然后显示在用户的屏幕上
什么是shell脚本
简单的说,当命令或者程序不在命令行执行,而是通过一个程序文件来执行,这个程序就被称为shell脚本
也就是在shell脚本里内置了多条命令,语句,循环控制,然后将这些命令一次性执行完毕,这种通过文件执行命令的方式成为非交互式
二)为什么使用shell脚本
1.适合处理操作系统底层的业务,有众多系统命令为其做支撑(还有文本处理三兄弟grep,sed,awk)
2.是哦i适合处理纯文本文件,linux中许多服务配置文件,启动脚本,都是纯文本(httpd,nfs,mysql,nginx,lvs)
3.linux系统脚本用shell开发更简单
二、脚本的编写
一)如何查看系统默认shell
法1:echo $SHELL
/bin/bash
法2:grep root /etc/passwd
二)shell脚本的建立
1.第一行:#!/bin/bash
指定解释器:由哪个程序来执行脚本内容
#!:幻数
注意:#!/bin/bash必须写在第一行,否则会被认为是注释
示例:打印"Hello westos"“Hello westos”
vim test.sh
#!/bin/bash
echo "Hello westos"
#!/bin/bash
echo "Hello redhat"
三)脚本的执行过程
1.先加载系统环境变量
怎么查看系统环境变量:env
2.一条一条命令执行,遇到子脚本,先执行子脚本,然后返回父脚本继续执行
四)脚本的执行方法
1.bash script.sh 或者sh script.sh(当脚本没有执行权限时)
2.path/script.sh 或者 ./script.sh(绝对路径,或当前目录下)
3.source script.sh 或者 . script.sh(需要传递变量或函数时使用)
五)脚本开发规范
1.注释:可以放在命令后,也可以自成一行
2.脚本信息:
#!/bin/bash
#Date:2018-12-14
#Author:westos-wsp
#Connect:wsp439@sina.com
#Desc:This script is for...
#Version:1.0
3.脚本名:最好以.sh结尾
三、变量
一)普通变量
1.怎么定义普通变量:
echo $a
''默认不解析
没有特殊要求的时候,字符串都加双引号,需要原样输出时加单引号
$0:获取脚本文件名,如果执行时包含路径,则输出脚本路径
##[root@server mnt]# cat westos.sh
#!/bin/bash
echo $0
$n(>0):接受后面接受变量的参数
[root@server mnt]# cat westos.sh
#!/bin/bash
echo $1 $2
[root@server mnt]# sh westos.sh hello westos
hello westos
[root@server mnt]# sh westos.sh hello redhat
hello redhat
[root@server mnt]# echo \${1..10} > westos.sh
[root@server mnt]# cat westos.sh
$1 $2 $3 $4 $5 $6 $7 $8 $9 $10
[root@server mnt]# sh westos.sh {1..10}
1 2 3 4 5 6 7 8 9 10
[root@server mnt]# sh westos.sh {a..z}
a b c d e f g h i a0
[root@server mnt]# sh westos.sh {a..z}
a b c d e f g h i j
$#:后面接的参数的个数
[root@server mnt]# cat westos.sh ]
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
echo $#
[root@server mnt]# sh westos.sh {1..100}
1 2 3 4 5 6 7 8 9
100
$?:表示上一条命令是否执行成功,是则输出0,否则输出非0
$$:获取当前shell进程号
二)变量的值的计算
1.expr命令
[root@server mnt]# a=123
[root@server mnt]# expr $a + 10
133
[root@server mnt]# expr $a - 10
113
[root@server mnt]# expr $a * 10
expr: syntax error
[root@server mnt]# expr $a \* 10
1230
[root@server mnt]# expr $a / 10
12
[root@server mnt]# expr $a % 10
3
2. [ ] 和 []和 []和(())表达式
[root@server mnt]# echo $[a+10]
20
[root@server mnt]# echo $[a-10]
0
[root@server mnt]# echo $[a*10]
100
[root@server mnt]# echo $[a/10]
1
[root@server mnt]# echo $[a%10]
0
[root@server mnt]# echo $((a+10))
20
[root@server mnt]# echo $((a-10))
0
3.let命令(let命令在执行后会保存新的值)
[root@server mnt]# let a+=10
[root@server mnt]# echo $a
20
[root@server mnt]# let a-=10
[root@server mnt]# echo $a
10
[root@server mnt]# let a*=10
[root@server mnt]# echo $a
100
[root@server mnt]# let a/=10
[root@server mnt]# echo $a
10
[root@server mnt]# let a%=10
[root@server mnt]# echo $a
0
4.小数运算工具bc
[root@server mnt]# echo "scale=4;1.23*4.56" | bc
5.6088
[root@server mnt]# echo "scale=2;1.23*4.56" | bc
5.60
[root@server mnt]# echo 1.2+3.4 | bc
4.6
[root@server mnt]# echo 1.23+4.56 | bc
5.79
练习:计算两个数的加减乘除
#!/bin/bash
read -t 5 -p "请输入两个整数:" a b
echo "a+b=$[a+b]"
echo "a-b=$[a-b]"
echo "a*b=$[a*b]"
echo "a/b=$[a/b]"
echo "a**b=$[a**b]"
echo "a%b=$[a%b]"
四、字符处理
1)grep,egrep
grep
-i ##忽略字母大小写
-v ##条件取反
-c ##统计匹配行数
-q ##静默,无任何输出
-n ##显示匹配结果所在的行号
2)基本元字符
基本元字符:^
egrep '^w|^f' wsp
基本元字符:. ##过滤非空行
egrep '.' wsp ##匹配所有
egrep -v '.' wsp ##过滤空行
egrep '^$' wsp ##过滤空行
基本元字符+
egrep 'f+' 1.sh ##输出包括f,ff即最少出现一次
基本元字符:?
egrep 'color(ful)?' 1.sh ##末尾的ful出现一次
基本元字符:{} ##
egrep '(we){3}' 1.sh ##匹配到的we最多3次
基本元字符:[]
egrep '(we)[ab]' 1.sh ##匹配we后跟a或者跟b的
egrep '[A-Z]' 1.sh ##匹配有从A到Z的大写字母
正则表达式
^ ##匹配开头
$ ##匹配结尾
. ##匹配任意字符
[] ##匹配中括号内的任意字符
[^] ##匹配不再括号内的任意字符
3)cut命令
cut -d : -f 1-3(1-3列) 1,7 /etc/passwd
cut -c 1,4 /etc/passwd
&&如果前面的命令执行成功,执行后面的命令
||失败的话,执行后面的命令
4)sort 命令:排序
sort
-n ##纯数字排序
-r ##倒序
-u ##去掉重复数字
-o ##导入到指定目录
-t ##指定分隔符
-k ##指定排序的列
如果不加的话,默认按照列排序
5)uniq命令:对重复字符处理
uniq
-u ##显示唯一的行(去重)
-d ##显示重复的行
-c ##每行显示一次并显示重复的次数
五、test命令
条件测试
test "$a" == "$b"等同于[ "$a" == "$b" ]
[ "$a" == "$b" ] 等于
[ "$a" != "$b" ] 不等于
[ "$a" -eq "$b" ] 等于
[ "$a" -ne "$b" ] 不等于
[ "$a" -le "$b" ] 小于等于
[ "$a" -ge "$b" ] 大于等于
[ "$a" -gt "$b" ] 大于
[ "$a" -lt "$b" ] 小于
[ "$a" -ne "$b" -a "$a" -gt "$b" ] ##-a必须条件都满足
[ "$a" -ne "$b" -o "$a" -gt "$b" ] ##-o或者条件至少满足一个
[ -z "$a" ] ##是否为空
[ -e "file" ] ##是否存在
[ -f "file" ] ##普通文件
[ -b "file" ] ##块设备
[ -S "file" ] ##socket套接字,提供访问路口
[ -c "file" ] ##字符设备
[ -L "file" ] ##软链接
练习:用户输入一个数,看是否在10以内
1.输入是否为空
2.是否在10以内
3.在10以内输入YES
否则NO
#!/bin/bash
[ -z "$1" ] && {
echo "please input a number!"
exit 1
}
[ "$1" -gt "0" -a "$1" -lt "10" ] && {
echo "YES"
}||{
echo "NO"
}