1.什么是shell?
shell 是linux的一个外壳,它包在linux内核的外面,为用户和内核之间的交互提供了一个接口。
当用户下达命令给操作系统时,实际上是把指令告诉shell,经过shell解释,处理后让内核作出相应的动作。
系统的回应和输出的信息也由shell处理,然后显示在用户的屏幕上。
2.什么是shell脚本?
当命令或者程序不在命令行执行,而是通过一个程序文件来执行,这个程序就称作 shell脚本。
也就是在shell脚本里内置了多条命令,语句,循环控制,然后将这些命令一次性执行完毕,这种通过文件执行命令的方式称为非交互式。
3.为什么使用shell脚本?
(1) 适合处理操作系统底层的业务,有众多系统命令为其做支撑
(2)适合处理纯文本文件,linux中许多服务配置文件,启动脚本,都是纯文本
(3)linux 系统脚本用shell 开发更简单。
4.如何查看系统默认shell?
方法一:cat /etc/passwd | head -1
![](https://i-blog.csdnimg.cn/blog_migrate/16f0a157bd06af7f07e358424011cb44.png)
方法二:grep root /etc/passwd
![](https://i-blog.csdnimg.cn/blog_migrate/62a7c82d700754663596ed90fa8d53ab.png)
方法三:echo $SHELL
################ 5. 脚本格式 ############################
#!/bin/bash -------指定解释器:由哪个程序来执行脚本内容
if [ ];then #!:幻数
fi
################ 6. 脚本执行方法 ######################
1. sh script.sh | bash script.sh 在没有执行权限时
2. path/script.sh | ./script.sh 绝对路径,当前目录下
3.source script.sh | . script.sh
sh 与 ./ 在执行完成脚本之后
. 会把执行完的函数值或变量值传回到父shell继续使用
############### 7. 变量 #########################
环境变量,系统已经定义好,不用自己定义
普通变量,需要自己定义
定义变量,需要双引号
定义普通变量a
a=hello
echo $a
单引号默认不解析------适合于原样输出
![](https://i-blog.csdnimg.cn/blog_migrate/a879ad6ff28d1eb13237b2c55c9ae928.png)
双引号------解析
![](https://i-blog.csdnimg.cn/blog_migrate/288345729de9af7276f61f39162551f6.png)
################## 8. 特殊变量 ########################
$0:
获取脚本文件名,如果执行时包含路径,则输出脚本路径
![](https://i-blog.csdnimg.cn/blog_migrate/b5ee8711ab3b182cd3f150fae4f3884d.png)
$n(n>0):
表示输出脚本后紧跟的N个字符串
![](https://i-blog.csdnimg.cn/blog_migrate/8124bdd0d997c11073986f273bc90a14.png)
[root@server mnt]# cat westos.sh
$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 j
$#:
后面接的参数的个数
$?:
检测上一条命令是否执行成功
0-----表示执行成功
非0---表示执行失败
$$
当前进程号
echo $$
############## 9. read 用法 ###################
[root@server mnt]# read str
westos hello
[root@server mnt]# echo $str
westos hello
[root@server mnt]# read -p "请输入一个整数:" i
请输入一个整数:10
########## 10. 将命令的结果赋值给变量 #########
方法一.[root@desktop mnt]# CMD=$(ls -l)
[root@desktop mnt]# echo $CMD
![](https://i-blog.csdnimg.cn/blog_migrate/b9324a905a8f6f01768cc217ba5d0a6d.png)
方法二.[root@sdesktop mnt]# i=$’ls -l‘
![](https://i-blog.csdnimg.cn/blog_migrate/7c69905f356ac74ebf9154756fb443ad.png)
#################### 11. 打包日志 #######################
date +%F--------显示年月日
tar zcf log_$(date +%F).tar.gz /var/log/ #打包
![](https://i-blog.csdnimg.cn/blog_migrate/6e3f68f675f4d34b5747237944a6526b.png)
############### 12. 计算变量的值 ##############
1.expr $a 取a的值 (乘法需要转义)
a=123
[root@server mnt]# expr $a + 10
133
[root@server mnt]# expr $a - 10
113
[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 命令,变量值会变化,注意:在执行后会保存新的值
[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
100
[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 (scale=n 表示保留n 位小数)
[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
5.加减乘除平方取余脚本
平方a**2
立方a**3
脚本:取a=10 b=20 对a b 分别做加减乘除平方取余等等:
#!/bin/bash
a=10
echo a=$a
b=20
echo b=$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]"
执行脚本:
脚本:
计算两个数的加减乘除,让用户输入:
#!/bin/bash
read -t 5 -p "please input number 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]"
echo "a%b=$[a%b]"
# - t 5 表示等五秒之后不输入,自动退出
执行脚本:
################# 13 文本处理 ##############
1.字符处理
grep , egrep
grep
-i ##忽略字母大小写
-v ##条件取反
-c #统计匹配行数
-q #静默,无任何输出
-n #显示匹配结果所在的行号
-q:
系统中是否存在172.25.254.250这个用户;有的话输出yes 没有的话输出no
[root@server mnt]# grep '172.25.254.250' /etc/hosts && echo 'YES' || echo 'NO'
172.25.254.250 content.example.com
YES
[root@server mnt]# grep -q '172.25.254.250' /etc/hosts && echo 'YES' || echo 'NO'
YES
-c:
统计含有/sbin/bash的有多少行
[root@server mnt]# egrep -c '/sbin/nologin' /etc/passwd
35
2. 基本原字符: ^ $
-m 匹配行数:
egrep -m10 '/sbin/nologin' /etc/passwd 找出/etc/passwd的/sbin/nologin 的10行
^ 匹配开头:
grep '^r' /etc/passwd /etc/passwd 以r开头的
$ 匹配结尾:
egrep 'sbin$' xys 查找xys 文件中以sbin结尾的行
3.匹配元字符:
. 过滤非空行
egrep '.' xys
过滤空行:
方法一:[root@server mnt]# egrep -v '.' xys
方法二: [root@server mnt]# egrep '^$' xys
4.基本元字符
1)+ :
[root@server ~]# egrep 'f+' 3.sh 一个至多个f
2)* :所有
3)? :
末尾的er最多出现一次,也可以没有
[root@server ~]# egrep 'stuf(er)?' 3.sh
4) 元字符:{ }:
egrep '(we){3}' day.sh #we紧挨出现三次的
[root@server ~]# egrep '(we){2,4}' 1.sh 出现2-4次
[root@server ~]# egrep '(we){3,}' 1.sh 出现3-无穷多次
[root@server ~]# egrep '(we)[ab]' 1.sh we 后跟a 或b
[root@server ~]# egrep '[A-Z]' 1.sh 有大写字母的行
##################### 14 .cut 命令 ##############
cut
-d 指定分隔符为:显示1-3列: cut -d : -f 1-3 /etc/passwd
-c 显示第一和第四个字符: -cut -c 1,4 /etc/passwd
-f 列
练习1:获取本机ip
方法一:
ifconfig eth0 | grep "inet" | cut -d " " -f 10
方法二:
ifconfig eth0 | grep "inet" | awk '{print $2}'
练习2:检测网络
题目描述:若可以ping通则输出 ip is up 不通则输出ip is down
ping -c1 -w1 172.25.254.$1 &> /dev/null && echo 172.25.254.$1 is up || echo 172.25.254.$1 is down
练习3:查看cpu前五进程
ps -ax -o %cpu,pid --sort=-%cpu | grep -v PID | head -n 5
############## 15 sort-----排序 ###################
sort
-n 纯数字排序
-r 倒叙
-u 去掉重复数字
-o 输出到指定文件中
-t 指定分隔符
-k 指定要排序的列
sort westos #默认一列一列排序
sort -n westos 按照数字大小排列
sort -u westos 去掉重复数字
sort -t : -k 2 westos 以冒号为分隔符,对第二列进行排序
########## 16. uniq对重复字符处理 #############
uniq
-u 显示唯一的行
-d 显示重复的行
-c 每行显示一次并统计重复次数
sort -n westos | uniq -d 显示重复的行
sort -n westos | uniq -u 显示不重复的行
sort -n 2.sh | uniq -d -c 显示重复的行,并统计个数
练习:导出 /tmp下最大的文件,并输出文件名 (-S 表示按大小输出)
ls -Sl /tmp/ | head -2 | cut -d " " -f 9
################# 17. test命令 ##################
test 1==2 等同于test [ 1 == 2 ] 等同于 test [ 1 - eq 2]
test "$a" == "$b" 等同于test [ "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" ] -a条件至少满足一个
[ -z "$a" ] 是否为空
[ -e "file" ] 是否存在
[ -f "file" ] 普通文件
[ -b "file" ] 块设备
[ -S "file" ] 套接字
[ -c "file" ] 字符设备
[ -L "file" ] 软连接
练习一:
判断文件是否为普通文件
#!/bin/bash
[ "$1" "/etc/paswd" ] && echo YES || echo NO
练习二:
用户输入一个数,是否在10以内
思路:
1.判断是否为空
2.是否在10以内
3,1<$a<10 -- > yes
4.$a>10 ---- > no
#!/bin/bash
[ -z "$1" ] && {
echo "please input a number!"
exit 1
}
[ "$1" -gt "0" -a "$1" -lt "10" ] && {
echo "YES"
}||{
echo "NO"
}
############### 18. sed ####################
stream editor-----一次处理一行内容,处理时把当前的行存储在临时缓存去,处理完后,输送到屏幕
sed [参数] ‘命令’ file
p 显示
d 删除
a 添加
c 替换
i 插入
1) p: 显示
sed -n '/^#/p' /etc/fstab 显示以#开头
sed -n '/\:/p' /etc/fstab 有冒号的行,冒号需要转义
sed -n '/\:/!p' /etc/fstab 没有冒号的行,冒号需要转义
sed -n '2,6p 显示2-6行
sed -n '2,6!p 不显示2-6行
2) d:删除
删除以UID 开头的: sed '/^UUID/d' /etc/fstab
删除空行: sed '/^$/d' /etc/fstab
删除以#开头的行: sed '/^#/d' /etc/fstab
删除指定行: sed '1,4d' /etc/fstab
3)a:添加
换行: sed '/hello/aworld' westos
不换行:sed 's/hello/hello world/g' westos
换行: sed 's/hello/hello\nworld/g' westos
4)c:替换
sed '/hello/chello world' westos
(不加-i 只展示出效果,真正文件并不替换)
5)i 在上面插入:
sed '/hello/chello world' westos
(不加-i 只展示出效果,真正文件并不替换)
[root@server mnt]# sed -i 's/nologin/westos/g' passwd
#s/表示开头
#/g表示全局
练习:安装http,改变默认端口为8080:
#!/bin/bash
yum insatll httpd -y &>/dev/null
sed -i "/^Listen /cListen $1" /etc/httpd/conf/httpd.conf
echo -e "Port has changed!!!"
echo "Now ,Port is $1!!"
systemctl restart httpd
####################### 19. awk #########################
awk处理机制:一行一行的读,根据模式一次从文件中抽取i一行文本,对这行文本进行切片,默认使用空白字符作为分隔符
[root@server mnt]# cat test
this | is | a | file
$1 $2 $3 $4
0代表一整行
awk '{print $1}' test
this
awk '{print $4}' test
file
0代表一整行
[root@localhost Desktop]# awk '{print $0}' test
this is a file
awk '{print $1,$2}' test 显示两个
this is
练习:
查看用户名字以及uid /etc/passwd
以冒号为分隔符,打印用户名和uid
awk -F ":" '{print $1,$3}' /etc/passwd
################ 20. 常用变量 ##############################
总结: awk '{print "第NR行",“有NF列”}' etc/passwd
awk -F: '{print NR,NF}' /etc/passwd 输出每次处理的行号以及当前以冒号为分隔符的字段个数
awk '{print FILENAME,NR}' /etc/passwd 输出文件名以及行号
BEGIN { } 读入第一行文本之前执行的语句,一般用来初始化操作
{ }: 逐行处理
END { } 处理完最后一行文本后执行,一般用来处理输出结果
awk 'BEGIN { a=34;print a+10 }' # 从a=34开始,输出a+10
第一行之前加REDHAT 末尾加WESTOS 打印行号和内容,以冒号为分隔符:
awk -F: 'BEGIN{print "REDHAT"} {print NR;print } END {print "WESTOS"}' passwd
#第一行之前加REDHAT 末尾加WESTOS 打印行号和内容,以冒号为分隔符:
输出以bash结尾的行
awk -F: '/bash$/{print}' /etc/passwd
# 输出以bash结尾的行
输出第三行
awk -F: 'NR==3 {print}' /etc/passwd
#输出第三行
输出偶数行
awk -F: 'NR % 2 == 0 {print}' /etc/passwd
#输出偶数行
输出前三行
awk -F: 'NR <= 3 {print}' /etc/passwd
#输出前三行
练习:
1.找出系统中uid<2用户的uid以及用户名 打印 /etc/passwd
awk -F: '$3 < 2 {print $1,$3}' /etc/passwd
2.打印出3---5行:
awk -F: 'NR >=3 && NR <=5 {print }' /etc/passwd
3.统计字段的字段数:
awk 'BEGIN{i=0}{i+=NF}END{print i}' linux.txt
cat linux.txt
ABSD XX
XX
ABSDABCD XX