一、什么是shell
shell也是操作系统中的一个软件,它包在linux内核的外面,为用户和内核之间的交互提供了一个接口,系统中的命令用shell去解释shell接受系统回应的输出并显示其到屏幕中
shell的类型如下图
**bash= GUN Bourne-Again Shell**
二、什么是shell脚本
脚本是一种解释型语言,用shell脚本保存执行的动作,用脚本判定命令的执行动作,用脚本来实现动作的批量执行。
*shell脚本hello jane如下图
第一行为幻术,指定shell的类型
*shell脚本的执行
1)sh 直接用指定解释器解释(什么都不加默认用shell),不读幻术,不需要加执行权限
2)./执行,用脚本写入的幻术解释器解释,需要加执行权限chmod +x
三、shell脚本的vim编写
在/etc/vimrc配置文件下,编写如下图
第66行map表示设置快捷键为[F4];ms调用什么东西 <cr>‘s表示自动执行
第67行表示自动运行,条件是新文件以.sh结尾的文件;自动运行
Author__脚本作者
CreateTime__脚本创作时间;.strftime表示捕捉时间
Mail__脚本作者联系方式(随意写的)
Version__脚本的版本
Description__脚本的描述
四、脚本的调试
sh -x 脚本名称 适用于所有shell脚本
显示出来+后面的表示命令
五、脚本中常用的命令
1、diff命令
*)diff命令是用来比较两个文件或目录的不同
[root@jane1 mnt]# diff westos westos1
2d1
< linux
[root@jane1 mnt]# cat westos1
hello 123
westos
[root@jane1 mnt]# diff westos westos1
2c2
< linux
---
> westos
[root@jane1 mnt]# cat westos
hello 123
[root@jane1 mnt]# diff westos westos1
1a2
> westos
a
表示添加
c
表示改变
d
表示删除
<
表示第一个文件中的内容,>
表示第二个文件的内容
*)diff -u 以合并的方式来显示文件内容的不同
[root@jane1 mnt]# diff -u westos westos1
--- westos 2018-06-09 23:06:15.305734996 -0400
+++ westos1 2018-06-09 23:05:38.598734996 -0400
@@ -1 +1,2 @@
hello 123
+westos
[root@jane1 mnt]# ls
westos westos1
[root@jane1 mnt]# diff -u westos westos1 > westos.path
[root@jane1 mnt]# cat westos.path
--- westos 2018-06-09 23:06:15.305734996 -0400
+++ westos1 2018-06-09 23:05:38.598734996 -0400
@@ -1 +1,2 @@
hello 123
+westos
**这里可以生成westos.path(补丁文件)
可以将第一个文件添加补丁使两个文件一致**
2、pacth命令
用于不同文件打补丁
[root@jane1 mnt]# yum install patch -y
[root@jane1 mnt]# ls
westos westos1 westos.path
[root@jane1 mnt]# patch -b westos westos.path
patching file westos
示例:指定IP是否存在
3、cut命令
用于命令多用与字符截取
cut -d
指定分隔符
cut -f
指定截取的列
cut -c
指定截取的字符位置
[root@jane1 mnt]# rm -fr *
[root@jane1 mnt]# cp /etc/passwd .
[root@jane1 mnt]# ls
passwd
[root@jane1 mnt]# vim passwd
[root@jane1 mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@jane1 mnt]# cut -d : -f 1 passwd
root
bin
[root@jane1 mnt]# cut -d : -f 1,2 passwd
root:x
bin:x
[root@jane1 mnt]# cut -d : -f 1-3 passwd
root:x:0
bin:x:1
[root@jane1 mnt]# cut -d : -f 1- passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@jane1 mnt]# cut -d : -f 3- passwd
0:0:root:/root:/bin/bash
1:1:bin:/bin:/sbin/nologin
[root@jane1 mnt]# cut -c 1,3 passwd
ro
bn
[root@jane1 mnt]# cut -c 1-3 passwd
roo
bin
实例:执行ifconfig eth0截取出ip
4、sort命令
多用与字符排序
sort -n
纯数字排序
sort -r
倒序
sort -u
去掉重复数字
sort -o
输出到指定文件
sort -t
指定分隔符
sort -k
指定要排序的列
[root@jane1 mnt]# vim linux
[root@jane1 mnt]# sort -n linux ##纯数字排序
[root@jane1 mnt]# sort -r linux ##倒序
[root@jane1 mnt]# sort -u linux ##去掉重复数字
[root@jane1 mnt]# sort -o linux linux1 ##输出指定文件中 (将linux1的内容代替linux的内容)
[root@jane1 mnt]# vim linux
[root@jane1 mnt]# sort -n -t : -k 2 linux ##-t指定分隔符 -k指定要排序的列
[root@jane1 mnt]# sort -n linux | uniq -u ##显示唯一的行
[root@jane1 mnt]# sort -n linux | uniq -d ##显示重复的行
[root@jane1 mnt]# sort -n linux | uniq -c ##每行显示一次宾统计重复次数
示例/mnt目录中最大文件的名称
[root@jane1 mnt]# vim check_file.sh ##编辑脚本
[root@jane1 mnt]# ls -Sl /mnt ##/mnt目录中文件大小不同
total 4
-rw-r--r-- 1 root root 342 Jun 13 07:14 check_file.sh
[root@jane1 mnt]# sh check_file.sh ##调用脚本可以找到最大的文件
check_file.sh
[root@jane1 mnt]#
5、&&和||
&&
用来执行条件成立后执行的命令
||
用来执行条件不成立后执行的命令
c
次数,w
等待时间
[root@jane1 mnt]# ping -c1 -w1 172.25.254.129 &> /dev/null
[root@jane1 mnt]# ping -c1 -w1 172.25.254.129 &> /dev/null && echo 172.25.254.129 is up || echo 172.25.254.129 is down
172.25.254.129 is up
[root@jane1 mnt]# ping -c1 -w1 172.25.254.666 &> /dev/null && echo 172.25.254.666 is up || echo 172.25.254.666 is down
172.25.254.666 is down
7、test命令
test命令和[ ]等同
test “
B” 等同[ “
B” ]
[ “
B” ] 等于
[ “
B” ] 不等于
[ “
B” ] 等于
[ “
B” ] 不等于
[ “
B” ] 小于等于
[ “
B” ] 小于
[ “
B” ] 大于等于
[ “
B” ] 大于
-a
代表与操作
-o
代表或操作
-z
为空[ -z “$A” ]
-n
不为空 [ -n “$A” ]
1)用test命令解决测试ip时不输入IP出现的bug
[root@jane1 mnt]# vim ip_show.sh
[root@jane1 mnt]# chmod +x /mnt/ip_show.sh
[root@jane1 mnt]# /mnt/ip_show.sh
plesae input ipaddr!!
[root@jane1 mnt]# /mnt/ip_show.sh 172.25.254.222
172.25.254.222 is down
[root@jane1 mnt]#
2)示例:判断一个数字是否在1到10之间:
3)-eq 等于-ne 不等于这两个是数学的表示方法-gt 大于-ge 大于等于-le 小于等于-lt 小于
4)可以满足多个条件 -a and 和-o or 或者
5)-ef 节点号是否一致
-nt是否比这个文件新(时间戳)
-ot 是否比这个文件老
[root@jane1 mnt]# ls
file file1
[root@jane1 mnt]# rm -fr file1
[root@jane1 mnt]# touch file1
[root@jane1 mnt]# [ "/mnt/file" -nt "/mnt/file1" ]&& echo yes || echo no
no
[root@jane1 mnt]# [ "/mnt/file" -ot "/mnt/file1" ]&& echo yes || echo no
yes
6)file命令
检测文件类型:
[ -e “file” ]是否存在
[ -f “file” ]是否为普通文件
[ -L “file” ]是否为软链接
[ -S “file” ]是否为套接字
[ -b “file” ]是否为块设备
[ -d “file” ]是否为目录
[ -c “file” ]是否为字符设备
[root@jane1 mnt]# sh file.sh -e ##文件是否存在
[root@jane1 mnt]# sh file.sh -f ##是否为正规文件
[root@jane1 mnt]# sh file.sh -L ##是否是符号链接
[root@jane1 mnt]# yum install mariadb-server -y ##测试套接字
[root@jane1 mnt]# systemctl start mariadb
[root@jane1 mnt]# rsync -D /var/lib/mysql/mysql.sock /mnt/file ##远程传输
[root@jane1 mnt]# sh file.sh -S ##是否为套接字
[root@jane1 mnt]# sh file.sh -s ##是否有容量
[root@jane1 mnt]# sh file.sh -b ##是否是块设备
[root@jane1 mnt]# sh file.sh -d ##是否为目录
[root@jane1 mnt]# sh file.sh -c ##是否为字符设备
示例:一个脚本检测/mnt/file的文件类型
[root@jane1 mnt]# vim file_check.sh
[root@jane1 mnt]# sh file_check.sh
Please input a file name after script!!
[root@jane1 mnt]# sh file_check.sh /var/lib/mysql/mysql.sock
/var/lib/mysql/mysql.sock is a taojiezi
[root@jane1 mnt]# sh file_check.sh /dev/vdb
/dev/vdb is a block file
8.tr命令
[root@localhost mnt]# tr 'a-z' 'A-Z' < test.sh
#!/BIN/BASH
[ "$1" = "HELLO" ]&& {
ECHO YES
}||{
ECHO NO
}
[root@localhost mnt]# tr 'A-Z' 'a-z' < test.sh
#!/bin/bash
[ "$1" = "hello" ]&& {
echo yes
}||{
echo no
}
示例:用脚本建立一个用户user1,密码为123,存在的话返回用户已经存在:
脚本一:
脚本二:
[root@localhost mnt]# vim user1_create.sh
[root@localhost mnt]# cat user1_create.sh
#!/bin/bash ##检测是否有两串字符
[ "$#" -eq "2" ]||{
echo "Please input username and password"
exit 1
}
check_user=`getent passwd $1` ##检测是否为用户
[ -n "$check_user" ]&&{
echo $1 is exist!!
exit 1
}
useradd $1 ##建立用户赋予密码
echo $2 | passwd --stdn $1
示例:转换大小写
先读 WOED
[root@jane1 mnt]# vim test.sh
[root@jane1 mnt]# sh test.sh hello
yes
[root@jane1 mnt]# sh test.sh HELLO
yes
9.$#命令
[root@localhost mnt]# vim test.sh