一. shell 脚本中的基本知识
1.什么是 shell
shell 是操作系统中的一个软件。它包在 linux 内核的外面,为用户和内核之间的
交互提供了一个接口,系统中的命令用 shell 去解释shell 接收系统回应的输出并
显示其到屏幕中。
二.脚本的编写
shell脚本
1.什么是shell脚本
脚本是一种解释型语言。用shell脚本保存执行动作;用脚本判定命令的执行条件;用脚本来实现动作的批量执行。
三.如何创建shell脚本
vim script.sh 用 vim 编写脚本
#!/bin/bash 脚本使用的解释器,通常用幻数 "#!" 指定
#AUTHOR 脚本作者
#DATE 脚本创作时间
#MAIL 脚本作者联系方式
#VERSION 脚本的版本
四.脚本的编写
1)编写脚本
1.cd /mnt/ 进入目录
2.vim westos.sh 编辑脚本
内容:
#!/bin/bash 幻数(执行脚本调用的解释器)
watch -n 1 date
2)执行脚本有两种方法:
1.sh westos.sh 执行脚本
执行结果:
2.chmod +x westos.sh 加可执行权限
/mnt/westos.sh 绝对路径的方式执行
执行结果:
3)脚本调试:
编辑脚本:vim westos.sh
内容:
#! /bin/bash -xecho
hello westos
用绝对路径执行后会显示执行脚本调用的解释器和执行结果
## + echo hello westos 执行脚本调用的解释器 ##
## hello westos 执行结果 ##
编辑脚本自动添加标题:
1. vim /etc/vimrc 编辑vim配置文件
内容:
67 autocmd BufNewFile *.sh exec ":call WESTOS()" 打开以.sh结尾
的新的文件会自动添加
68 " map <F4> ms:call WESTOS()<cr>'s 打开文件按F4会自动添加标题
69
70 function WESTOS()
71 call append(0,"#####################################")
72 call append(1,"# Author : lee #")
73 call append(2,"# Mail : lee@westos.com #")
74 call append(3,"# Viersion : 1.0 #")
75 call append(4,"# Create_Time : ".strftime("%Y-%m-%d")." #")
76 call append(5,"# Description : #")
77 call append(6,"# #")
78 call append(7,"# #")
79 call append(8,"#####################################")
80 call append(9,"")
81 call append(10,"#!/bin/bash")
82 endfunction
执行结果:vim file.sh
脚本示例:编辑脚本显示ip
1.vim ip_show.sh 编辑脚本显示ip
内容:
#####################################
# Author : lee #
# Mail : lee@westos.com #
# Viersion : 1.0 #
# Create_Time : 2018-05-14 #
# Description : #
# #
# #
#####################################
#!/bin/bash
ifconfig eth0 | awk -F " " '/inet /{print $2}' 显示ip
2.sh ip_show.sh 执行
编辑脚本显示主机用户:
1.vim use_show.sh 编辑脚本
内容:
#####################################
# Author : lee #
# Mail : lee@westos.com #
# Viersion : 1.0 #
# Create_Time : 2018-05-09 #
# Description : #
# #
# #
#####################################
#!/bin/bash
awk -F : '/bash$/{print $1}' /etc/passwd
2.sh use_show.sh 执行脚本
五.脚本命令
diff 比较两文件的不同
uniq 对重复字符做相应的处理,uniq配合sort用
patch 打补丁
tr 字符转换(文件内容)
cut 字符截取
test 和[]等同
sort 字符排列
&&和|| 是与非,&& 用来执行条件成立后执行命令。
|| 用来执行条件不成立后执行命令。
1. diff 比较两文件的不同
diff在比较文件过程中结果读取方式:
[num1,num2]a|c|d[num3,num4]
num1,num2表示在第一个文件的行数
a表示添加:add
c表示更改:change
d表示删除:delete
num3,num4 表示在第二个文件中的行数
示例:
1.cd /mnt 进入目录建立文件
2.vim westos :123 建立文件westos写入内容:123
vim westos1 : 123 建立文件westos1写入内容:123
456 456
用diff比较两个的文件的不同:表示第一个文件的第二行在加上456就和第二个文件一样。
打补丁:
1.yum install patch -y 安装打补丁软件
2.diff -u westos westos1 > file.path 生成补丁文件
3.patch westos file.path 打补丁(会覆盖源文件内容)
4.patch -b westos file.path 打补丁不会覆盖源件
file.path westos westos1 westos.orig
补丁文件 打补丁后的文件 原文件 原文件
5.patch -r dir1 dir2 比较两个目录的不同
2. cut 用于字符截取
cut -d 指定分隔符
cut -f 1,7|1-7 指定截取的列
cut -c 1,4|1-4 指定截取的字符位置
3. sort 用于字符排列
sort -n 纯数字排列
sort -r 倒序
sort -u 去掉重复数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定排序的列
示例:ls -l /mnt/ | sort -t ” ” -k 5 -n
4.uniq 对重复字符做相应的处理,uniq配合sort用
uniq 对重复字符做相应的处理
uniq -u 显示唯一的行
uniq -d 显示重复的行
uniq -c 每行显示一次并统计重复次数
示例:
vim hello 编辑文件写入内容:
处理结果:
5. &&和|| 是与非。
&& 用来执行条件成立后执行命令。
|| 用来执行条件不成立后执行命令。
示例: vim check_ip.sh 编辑脚本ping “IP”通的显示up不通显示down
内容:
#####################################
# Author : lee #
# Mail : lee@westos.com #
# Viersion : 1.0 #
# Create_Time : 2018-05-09 #
# Description : #
# #
# #
#####################################
#!/bin/bash
ping -c1 -w1 $1 &>/dev/null && echo $1 is up || echo $1 is down
执行结果:
6.test 和[ ]等同
test "$A"="$B" 等同 ["$A"="$B"]
["$A"="$B"] 表示"$A"="$B"成立
["$A"!="$B"] 表示"$A"="$B"不成立
[ -z "$A"] 表示$A是否为空
[ -n "$A"] 表示$A是否不为空
示例:
整型数据比较:
参数:
-eq 等于
-ne 不等于
-le 小于等于
-lt 小于
-ge 大于等于
-gt 大于
示例:
a=10 给a赋值
b=20 给b赋值
[ "$a" -eq "$b" ] && echo yes || echo no $a等于$b成立输出yes不成立输出no
[ "$a" -ne "$b" ] && echo yes || echo no $a不等于$b成立输出yes不成立输出no
[ "$a" -le "$b" ] && echo yes || echo no $a小于等于$b成立输出yes不成立输出no
[ "$a" -lt "$b" ] && echo yes || echo no $a小于$b成立输出yes不成立输出no
[ "$a" -ge "$b" ] && echo yes || echo no $a大于等于$b成立输出yes不成立输出no
[ "$a" -gt "$b" ] && echo yes || echo no $a大于$b成立输出yes不成立输出no
[ "$a" = "$b" -o "$a" -lt "50" ] $a是否等于$b,或者$a小于50
[ "$a" = "$b" -a "$a" -lt "50" ] $a是否等于$b,并且$a小于50
-o 表示或者
-a 表示并且
示例:
编辑脚本:vim check_num.sh 判定一个数字是否在0-10之间,脚本后面没数字显示please give me a number!,有数字进行判定在1-10输出yes否则输出no。
#!/bin/bash
[ -z "$1" ] && { 判定脚本后第一穿字符是否为空
echo please give me a number !!
exit 1
}
[ "$1" -gt "0" -a "$1" -lt "10" ] && echo yes || echo no
执行结果:
示例:
编辑脚本:vim clean_log.sh 清空日志,如果不是超级用户输出您不是超级用户!,如果是超级用户执行清空日志
内容:
#!/bin/bash
User_ID=`id -u` 变量
[ "$User_ID" -ne "0" ] && { uid不等于0
echo 您不是超级用户!
exit 1
}
> /var/log/messages
执行结果:
文件比较:
[ "file1" -ef "file" ] 节点是否一致,即文件是否一样
[ "file1" -ef "file" ] file1是否比file2时间新
[ "file1" -ef "file" ] file1是否比file2时间老
示例:
touch linux 建立文件
ls -i linux 查看文件节点号
ln /mnt/linux /mnt/linux1 创建硬链接
ls -i /mnt/linux /mnt/linux1 查看硬链接两个文件的节点号
判断结果:
[ "/mnt/linux" -ef "/mnt/linux1" ] && echo yes || echo no
判断两个文件节点号是否一致
[ "/mnt/linux" -nt "/mnt/passwd" ] && echo yes || echo no
判断linux文件是否比passwd文件新出现
[ "/mnt/linux" -ot "/mnt/passwd" ] && echo yes || echo no
判断linux文件是否比passwd文件早
test命令:判定文件的类型
参数:
[ -e "file" ] 文件是否存在
[ -f "file" ] 文件是否为普通文件
[ -L "file" ] 文件时否为连接(软连接)
[ -S "file" ] 文件是否为套接字
[ -b "file" ] 文件是否为快设备
[ -d "file" ] 文件是否为目录
[ -c "file" ] 文件是否为字符设备
ln -S /mnt/file /mnt/hello 创建链接-s表示软链接,不加-s表示硬链接
示例:
编写脚本:vim file.sh 判定文件是什么类型的文件
内容:
#!/bin/bash
[ -z "$1" ] && { 脚本后是否为空
echo please give me a filename !! 为空输出please give me a filename !!
exit 1
}
[ -e $1 ] || { 文件是否存在
echo $1 is not file 文件不存在
}
[ -L $1 ] && { 文件是否为连接
echo $1 is a link file 文件是连接
exit 1
}
[ -f $1 ] && { 文件是否为普通文件
echo $1 is a common file 文件是普通文件
}
[ -S $1 ] && { 文件是否为套接字
echo $1 is a sock file 文件是套接字
}
执行结果:
7. tr 字符转换(文件内容)
tr 'a-z' 'A-Z' < /mnt/westos > /mnt/hello
将westox内容转化成大写并重定向到/mnt/hello中
示例:
8.find 查找
参数:
-type 类型
-size 大小
-perm 权限
-user 拥有着
-group 所有组
-name 名字
-mindepth 最小深度
-maxdepth 最大深度
示例:
find /mnt/ -type l 查找/mnt/的连接的文件
find /mnt/ -group student 查找/mnt/的student组的文件
find /mnt/ -user root -a -group student
查找/mnt/的文件是root用户并且是student组的文件
find /mnt/ -user root -o -group student
查找/mnt/的文件是root用户或者是student组的文件
find /mnt/ -perm 权限查找
例如:
find /mnt/ -perm 444 权限查找(查找/mnt下权限为444的文件)
find /mnt/ -perm /444 权限查找(u位有4或者g位有4或者o位有4) /表示或者
find /mnt/ -perm -444 权限查找(u位有4并且g位有4并且o位有4) -表示并且
find /mnt/ -size 文件的大小查找
示例:
dd if=/dev/zero of=/mnt/file1 bs=1024 count=10 截取文件
dd if=/dev/zero of=/mnt/file2 bs=1024 count=20 截取文件
dd if=/dev/zero of=/mnt/file3 bs=1024 count=30 截取文件
find /mnt/ -size 20k 查找大小为20k的文件
find /mnt/ -size -20k 查找大小小于20k的文件
find /mnt/ +size -20k 查找大小大于20k的文件
find /etc -maxdepth 深度查找
find /etc -maxdepth 1 -name passwd 查找深度位1的文件
find /etc -mindepth 1 -maxdepth 2 -name passwd
查找最小深度位1最大深度位2的文件