关于脚本
脚本是解释性语言,脚本内容需要解释器解释,不需要编译可以直接用,但运行效率低,用于记录系统命令,是以文本形式存在的
C和JAVA等是描述性语言,是将动作描述给系统,运行效率高
shell脚本
名称:
通常以.sh 结尾
基本内容:
Author:开发者
Version:版本
Mail:邮箱
Date:时间
Description: 描述
除了以上基础内容,文件还必须以 #!/bin/bash 开始 ,其中 #!告诉系统后面的参数是用来执行文件的,/bin/bash执行文件,#!/bin/bash 是幻数,在脚本运行时先运行bash程序
当需要写注释时,以#开头
执行方法:
1、chmod +x filename.sh 加可执行权限
./filename.sh 执行脚本 或者 使用绝对路径执行脚本
2、sh xx.sh 直接执行脚本文件
shell脚本的常见编译器:bash、sh。
实验:
[root@node1 ~]# cd /mnt
[root@node1 mnt]# vim ip_show.sh
文件内容如下:
执行该脚本:
给脚本自动添加前面的脚本解释内容
[root@node1 Desktop]# vim /etc/vimrc
注意:这里F4也可以换成其他的键,只要这个按键没有其他快捷作用时。
函数设定:
func 函数名( )
内容
endfunc
[root@node1 Desktop]# vim file.sh #当是map...时按F4文件会出现以下内容,当是autocmd时打开第一次文件就可以看到以下内容
shell 基础命令
1、diff
diff file1 file2 比较文件file1和file2的不同
a 表示添加
c 表示改变
d 表示删除
1 a|c|d 1 前面的1表示第一个文件内容第一行,后面的1表示第二个文件的第1行
关于目录的打补丁(-r表示对目录进行补丁):
2、patch
[root@node1 mnt]# cat haha1
hello
[root@node1 mnt]# cat haha2
hello
haha
[root@node1 mnt]# diff haha2 haha1
2d1
< haha
[root@node1 mnt]# diff -u haha2 haha1 #显示补丁过程
--- haha2 2018-06-13 10:20:52.750946910 -0400
+++ haha1 2018-06-13 10:21:51.844946910 -0400
@@ -1,2 +1 @@
hello
-haha
[root@node1 mnt]# diff -u haha2 haha1 > haha.path #将补丁过程导入.path文件
[root@node1 mnt]# cat haha.path #查看文件内容
--- haha2 2018-06-13 10:20:52.750946910 -0400
+++ haha1 2018-06-13 10:21:51.844946910 -0400
@@ -1,2 +1 @@
hello
-haha
[root@node1 mnt]# yum install patch -y #下载补丁工具,patch --help 可以查看相关命令及使用规则
[root@node1 mnt]# ls
file.sh haha1 haha2 haha.path ip_show.sh
[root@node1 mnt]# patch haha2 haha.path #对文件进行补丁
patching file haha2
[root@node1 mnt]# cat haha2
hello
[root@node1 mnt]# cat haha1
hello
[root@node1 mnt]# vim haha2 #修改文件内容,为了重新生成补丁
[root@node1 mnt]# cat haha2
hello
123
[root@node1 mnt]# diff -u haha2 haha1 > haha.path ##将补丁过程导入.path文件
[root@node1 mnt]# patch -b haha2 haha.path #-b保留原文件,并进行补丁
patching file haha2
[root@node1 mnt]# ls #查看有生成的原文件
file.sh haha1 haha2 haha2.orig haha.path ip_show.sh
[root@node1 mnt]# cat haha1
hello
[root@node1 mnt]# cat haha2
hello
3、cut
-d 匹配符号
-f 1-3 显示1-3列
-c 1-3 显示第一到三的字符
[root@node1 mnt]# cp /etc/passwd . #以便实验将passwd文件复制
[root@node1 mnt]# ls
file.sh ip_show.sh linux passwd unix
[root@node1 mnt]# vim passwd #删除一部分内容
[root@node1 mnt]# cat passwd #查看文件内容
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
[root@node1 mnt]# cut -d : -f 1-2 passwd #匹配符号 :并显示1、2列
root:x
bin:x
daemon:x
adm:x
lp:x
sync:x
[root@node1 mnt]# cut -d : -f 1,3 passwd #匹配符号 : 并显示1,3列
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
[root@node1 mnt]# cut -c 1-5 passwd #显示第1-5个字符
root:
bin:x
daemo
adm:x
lp:x:
sync:
脚本实验:执行脚本文件只显示ip
文件内容如下,共有三种:
1、此文件内容不够严谨,当ip不在第二行时就显示不出
2、优化后文件,grep查看含有inet的内容,查看含有inet那行第10列就是ip
3、再次优化,利用awk报告生成器
4、&&和| |
&& 表示true,在条件成立时使用
| | 表示false,在条件不成立时使用
[root@node1 mnt]# ping -c1 -w1 172.25.254.69 &> /dev/null && echo this ip up || echo this is down
#c代表次数,w代表等待时间
this ip up
脚本实验:
[root@node1 mnt]# vim check_ip.sh
[root@node1 mnt]# sh check_ip.sh 172.25.254.69
172.25.154.69 is up
5、sort
多用于字符排序
sort -n 纯数字排序
sort -r 倒序
sort -u 去掉重复的数字
sort -o 输出到指定文件
sort -t 指定分隔符
sort -k 指定要排序的列
[root@node1 mnt]# vim number
[root@node1 mnt]# cat number
1
3
5
9
33
2
54
3
4
[root@node1 mnt]# sort -n number #纯数字排序
1
2
3
3
4
5
9
33
54
[root@node1 mnt]# sort -rn number #倒序
54
33
9
5
4
3
3
2
1
[root@node1 mnt]# sort -rn -u number #去掉重复的数字并倒序
54
33
9
5
4
3
2
1
[root@node1 mnt]# sort -rn number -o file #将倒序数字输出到文件file
[root@node1 mnt]# cat file #查看导入内容
54
33
9
5
4
3
3
2
1
[root@node1 mnt]# vim file #修改file内容
[root@node1 mnt]# cat file #查看修改后内容
2:54
4:33
3:9
2:5
4:4
5:3
7:3
:2
:1
[root@node1 mnt]# sort -t : -k 1 file #指定分隔符:对第一列排序
:1
:2
2:5
2:54
3:9
4:33
4:4
5:3
7:3
6、uniq
对重复字符作相应的处理
uniq -u 显示唯一的行
uniq -d 显示重复的行
uniq -c 每行显示一次并统计重复次数
[root@node1 mnt]# vim file #编写文件内容
[root@node1 mnt]# cat file #对文件内容进行查看
2:54
4:33
3:9
4:4
4:4
5:3
5:3
:2
:1
[root@node1 mnt]# uniq -u file #显示文件唯一的行
2:54
4:33
3:9
:2
:1
[root@node1 mnt]# uniq -d file #显示文件重复的行
4:4
5:3
[root@node1 mnt]# uniq -c file #文件内容每行显示一次并统计重复次数
1 2:54
1 4:33
1 3:9
2 4:4
2 5:3
1 :2
1 :1
7、test
和[ ]等同
test “$A” == “$B” 等同 [ “$A” == “$B” ]
[ “$A” = “$B” ]
[ “$A” != “$B” ]
[ “$A” -eq “$B” ] 等于
[ “$A” -ne “$B” ] 不等于
[ “$A” -le “$B” ] 小于等于
[ “$A” -lt “$B” ] 小于
[ “$A” -ge “$B” ] 大于等于
[ “$A” -gt “$B” ] 大于
[ “$A” -ne “$B” -a “$A” -gt “$B” ] ne表示不等于,a表示and都满足
[ “$A” -ne “$B” -o “$A” -gt “$B” ] o表示or至少满足一个
[ -z “$A” ] 表示值为空
[ -n “$A” ] 表示值不为空
[ “file1” -ef “file2” ] ef 表示文件是否为同一个文件
[ “file1” -nt “file2” ] 表示new,file1是否比file2文件新
[ “file1” -ot “file2” ] 表示old,file1是否比file2文件老
注意:在编写文档时注意[ ]里面的内容前后要有空格,“”必须是英文的,不然在执行脚本时会报错。
[root@node1 mnt]# a=1
[root@node1 mnt]# b=2
[root@node1 mnt]# test "$a" == "$b" && echo yes || echo no #判断a和b是否相等
no
[root@node1 mnt]# [ "$a" == "$b" ] && echo yes || echo no
no
[root@node1 mnt]# ls
check_ip.sh file file.sh ip_show.sh linux number passwd unix
[root@node1 mnt]# ln /mnt/file /mnt/file1 #创建硬连接
[root@node1 mnt]# ls -li * #查看连接
11759340 -rw-r--r-- 1 root root 363 Jun 13 10:49 check_ip.sh
11759361 -rw-r--r-- 2 root root 36 Jun 13 10:56 file
11759361 -rw-r--r-- 2 root root 36 Jun 13 10:56 file1
11759332 -rw-r--r-- 1 root root 2 Jun 13 10:06 file.sh
11759341 -rwxr-xr-x 1 root root 60 Jun 13 10:39 ip_show.sh
11759348 -rw-r--r-- 1 root root 20 Jun 13 10:52 number
11759343 -rw-r--r-- 1 root root 215 Jun 13 10:41 passwd
linux:
total 0
33572969 -rw-r--r-- 1 root root 0 Jun 13 10:30 haha
unix:
total 0
[root@node1 mnt]# [ "/mnt/file" -ef "/mnt/file1" ] && echo yes || echo no
#判断文件是否为硬链接
yes
[root@node1 mnt]# [ "/mnt/file" -ef "/mnt/number" ] && echo yes || echo no
no
[root@node1 mnt]# [ "/mnt/file" -ot "/mnt/number" ] && echo yes || echo no
#判断file是否比number文件老
no
[root@node1 mnt]# [ "/mnt/file" -nt "/mnt/number" ] && echo yes || echo no
#判断file是否比number文件新
yes
脚本实验:输入一个数与10比较大小
[root@node1 mnt]# vim num_check.sh
测试:
8、test测试文件类型
-e 表示存在
-f 普通文件
-L 软连接,在脚本文件中写判断文件类型时,将软连接判断写在普通文件判断前面,因为软连接也是一种文本文件
-S 套接字
-b 块设备
-d 目录
-c 字符设备
[root@node1 mnt]# vim file.sh
文件内容如下:
[root@node1 mnt]# ls
file.sh
[root@node1 mnt]# sh file.sh -e #判断file是否存在
no
[root@node1 mnt]# touch file
[root@node1 mnt]# sh file.sh -e
yes
[root@node1 mnt]# sh file.sh -f #判断file是否为普通文件
yes
[root@node1 mnt]# sh file.sh -L #判断file是否为软连接
no
[root@node1 mnt]# ln -s /mnt/file.sh file #创建软连接
[root@node1 mnt]# sh file.sh -L
yes
[root@node1 mnt]# sh file.sh -S #判断file是否为套接字
no
[root@node1 mnt]# yum install mariadb-server -y #下载mariadb服务软件
[root@node1 mnt]# rsync -D /var/lib/mysql/mysql.sock /mnt/file #复制mysql套接字文件到file
[root@node1 mnt]# sh file.sh -S
yes
[root@node1 mnt]# sh file.sh -b #判断file是否为块设备
no
[root@node1 mnt]# rsync -D /dev/vdb /mnt/file #将设备vdb复制到文件file
[root@node1 mnt]# sh file.sh -b
yes
[root@node1 mnt]# sh file.sh -d #判断file是否是目录
no
[root@node1 mnt]# rm -f file #删除file
[root@node1 mnt]# mkdir file #创建目录file
[root@node1 mnt]# sh file.sh -d
yes
[root@node1 mnt]# sh file.sh -c #判断file是否为字符设备
no
[root@node1 mnt]# rsync -D /dev/pts/ /mnt/file #输入键放在/dev/pts后按Tab查看字符设备
0 ptmx
[root@node1 mnt]# rsync -D /dev/pts/0 /mnt/file #将字符设备同步到file
[root@node1 mnt]# sh file.sh -c
yes
9、tr
tr 转换字符大小
格式:tr 参数1 参数2 标准输入
[root@node1 mnt]# vim file
[root@node1 mnt]# cat file
abc
def
[root@node1 mnt]# tr 'a-z' 'A-Z' < file #将文件小写字符转换为大写
ABC
DEF
[root@node1 mnt]# tr 'A-Z' 'a-z' < file #将文件大写字符转换为小写
abc
def
脚本实验:
判断执行脚本时输入的内容和hello是否相等
[root@node1 mnt]# vim haha.sh
出现上面的情况是因为执行判断时不能自动转换字母的大小写,以致输入大写时会出现不相等的情况,这时就需要 tr 命令对输入的内容进行转义
[root@node1 mnt]# vim haha.sh
修改后文件内容为:
然后进行判断大小写都可以:
10、shell 脚本排错
[root@node1 mnt]# vim haha.sh
文件内容如下(将命令ifconfig写错):
执行文件会进行报错但是不知道具体时间那个命令写错了,这时加 -x 参数观察运行情况,会对具体命令以及行数进行报错。
查看脚本内容的具体错误提示,根据提示进行相应修改即可
脚本建立用户
步骤:
1、查看脚本后所跟字符是不是两串
2、查看用户是否存在,如果不存在
3、建立不存在的用户并设定密码
[root@node1 mnt]# vim user_create.sh
文件内容如下:
测试执行脚本时后面不跟字符,以及跟字符建立用户,和再次建立用户: