shell脚本
编辑shell脚本一般以.sh结尾
[root@foundation77 ~]# cd /ost
[root@foundation77 ost]# ls
[root@foundation77 ost]# vim westos.sh 编辑一个shell脚本
1 #!/bin/bash (幻数 shell脚本开头必须要有)
1.[root@foundation77 ost]# sh westos.sh
hello westos
2.[root@foundation77 ost]# chmod +x westos.sh 添加一个可执行权限
[root@foundation77 ost]# /mnt/westos.sh
hello westos
以上两种方式都可以看到shell脚本执行的结果
如果要看他的命令及结果,也有两种方式
1.[root@foundation77 ost]# sh -x /mnt/westos.sh
+ echo hello westos (执行的命令)
hello westos (执行的结果)
2.[root@foundation77 ost]# vim westos.sh
1 #!/bin/bash -x (最后加入-x)
2 echo hello westos 保存退出
[root@foundation77 ost]# /mnt/westos.sh
+ echo hello westos
hello westos (此时可以查看到命令及结果)
一般来说,脚本需要有作者,说明及创建时间等,如果每次编写则太过麻烦,可以写在系统文件中自动读取 有两种编辑方式
1.(第一种方法)
[root@foundation77 ost]# vim /etc/vimrc 编辑此文件
67 map <F4> ms:call WESTOS()<cr>'s (编辑文件按F4自动执行WESTOS命令)
68 function WESTOS() (68及以下为命令内容)
69 call append(0,"#####################################")
70 call append(1,"# Author : lee #")
71 call append(2,"# Mail : lee@westos.com #")
72 call append(3,"# Version : 1.0 #")
73 call append(4,"# Create_Time : ".strftime("%Y-%m-%d")." #")
74 call append(5,"# Description : #")
75 call append(6,"# #")
76 call append(7,"# #")
77 call append(8,"#####################################")
78 call append(9,"")
79 call append(10,"#!/bin/bash")
80 endfunction
1 #####################################
2 # Author : lee #
3 # Mail : lee@westos.com #
4 # Version : 1.0 #
5 # Create_Time : 2018-05-09 #
6 # Description : #
7 # #
8 # #
9 #####################################
10
11 #!/bin/bash
12 #!/bin/bash -x
13 echo hello westos 按F4后会出现刚才编辑的表头
2.第二种方法
[root@foundation77 ost]# vim /etc/vimrc
67 autocmd BufNewFile *.sh exec ":call WESTOS()" (当编辑一个新的以.sh结尾的shell时,会出现WESTOS表头)
68 "map <F4> ms:call WESTOS()<cr>'s (使用"注释掉本行命令)
69 function WESTOS()
70 call append(0,"#####################################")
71 call append(1,"# Author : lee #")
72 call append(2,"# Mail : lee@westos.com #")
73 call append(3,"# Version : 1.0 #")
74 call append(4,"# Create_Time : ".strftime("%Y-%m-%d")." #")
75 call append(5,"# Description : #")
76 call append(6,"# #")
77 call append(7,"# #")
78 call append(8,"#####################################")
79 call append(9,"")
80 call append(10,"#!/bin/bash")
81 endfunction 保存退出
[root@foundation77 ost]# vim westos.sh
1 #####################################
2 # Author : lee #
3 # Mail : lee@westos.com #
4 # Version : 1.0 #
5 # Create_Time : 2018-05-09 #
6 # Description : #
7 # #
8 # #
9 #####################################
10
11 #!/bin/bash
此时不需要按F4 会自动弹出表头 (必须编辑新的以.sh结尾的shell才可以,编辑以前存在的则不会出现)
执行ip_show.sh 显示当前主机的ip地址
[root@foundation77 ost]# vim ip_show.sh
12 ifconfig br0 | awk -F " " '/inet\>/{print $2}' 加入此行 保存退出
[root@foundation77 ost]# chmod +x ip_show.sh
[root@foundation77 ost]# sh ip_show.sh
172.25.22.77
[root@foundation77 ost]# vim user_show.sh
12 awk -F : '/bash$/{print $1}' /etc/passwd 加入此行 保存退出
[root@foundation77 ost]# chmod +x user_show.sh
[root@foundation77 ost]# /mnt/user_show.sh
root
kiosk
shell中的命令
[root@foundation77 ost]# vim westos
[root@foundation77 ost]# cat westos
123
[root@foundation77 ost]# vim westos1
[root@foundation77 ost]# cat westos1
123
[root@foundation77 ost]# diff westos westos1
1a2 (此行表示第一个文件的第二行中添加下面第二个文件中多出来的内容)
> 456 (第二个文件中多出来的内容)
diff在比较文件过程中的结果读取方式
[num1,num2][a|c|d][num3,num4]
num1,num2 表示在第一个文件中的行数
a表示添加-----add c表示更改-------change d表示删除-----delete
<表示第一个文件中的内容,>表示第二个文件中的内容
num3,num4表示在第二个文件中的行数
比较出不同可以用来做补丁
[root@foundation77 ost]# diff -u westos westos1 >file.path 把两个不同导入到file.path文件
下载patch服务
[root@foundation77 ost]# yum install patch -y
[root@foundation77 ost]# patch westos file.path 添加补丁
patching file westos
[root@foundation77 ost]# cat westos
12
[root@foundation77 ost]# cat westos1
12 此时两个文件一样
也可以备份原文件再添加补丁
[root@foundation77 ost]# patch -b westos file.path
patching file westos 备份原westos文件
[root@foundation77 ost]# ls
file.path westos westos1 westos.orig (备份文件)
cut
cut命令用于字符截取
cut -d 指定分隔符 cut -f 1,7|1-7 指定截取的列 cut -c 1,4|1-4 指定截取的字符位置
[root@foundation77 ost]# vim user_show.sh
grep bash /etc/passwd |cut -d : -f 1 指定分隔符为“:” 截取的列为第一列
[root@desktop22 mnt]# chmod +x user_show.sh
[root@foundation77 ost]# /ost/user_show.sh
root
12 ifconfig eth0 | head -n 2 | tail -n 1 | cut -d " " -f 10 保存退出
[root@foundation77 ost]# chmod +x ip_show.sh
[root@foundation77 ost]# /ost/ip_show.sh
172.25.22.10 此时可以看到ip
sort 多用于字符排列
sort -n 纯数字排列
sort -r 倒叙
sort -u 去掉重复数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定要排序的列
[root@foundation77 ost]# ll
total 32
-rw-r--r--. 1 root root 119 Jun 14 10:44 file.path
-rwxr-xr-x. 1 root root 401 Jun 14 09:58 ip_show.sh
-rw-r--r--. 1 root root 411 Jun 14 11:16 ips_show.sh
-rwxr-xr-x. 1 root root 396 Jun 14 11:00 user_show.sh
-rwxr-xr-x. 1 root root 392 Jun 14 11:02 usr_show.sh
-rw-r--r--. 1 root root 4 Jun 14 10:15 westos
-rw-r--r--. 1 root root 8 Jun 14 10:15 westos1
-rw-r--r--. 1 root root 376 Jun 14 09:33 westos.sh
当前目录中的文件,按从大到小排序
[root@foundation77 ost]# ls -l /mnt | sort -t " " -k 5 -rn
-rw-r--r--. 1 root root 266 May 16 08:32 useradd.sh
-rwxr-xr-x. 1 root root 223 Apr 21 15:43 vm_create.sh
-rw-r--r--. 1 root root 18 May 16 08:18 userfile
-rw-r--r--. 1 root root 12 May 16 08:18 passwdfile
total 16
先列出文件做为后面的输出项,-t指定分隔符为“空格” 然后指定要排序的列为第五列,然后倒序排出
uniq 对重复字符做相应的处理 (搭配sort使用)
uniq -u 显示唯一的行
uniq -d 显示重复的行
uniq -c 每行显示一次并统计重复次数
[root@foundation77 ost]# cat westos3
1
2
4
8
6
5
9
6
7
3
4
7
8
5
11
[root@foundation77 ost]# sort -n westos3 | uniq -d 显示重复的行排列
4
5
6
7
8
[root@foundation77 ost]# sort -n westos3 | uniq -c 每行显示一次并统计重复次数
1 1
1 2
1 3
2 4
2 5
2 6
2 7
2 8
1 9
1 11
[root@foundation77 ost]# sort -n westos3 | uniq -u 显示唯一的行并排序
1
2
3
9
11
&&和||
&&用来执行条件成立后执行的命令 ||用来执行条件不成立后执行的命令
[root@foundation77 ost]# vim check_ip.sh
12 ping -c1 -w1 $1 &>/dev/null && echo $1 is up || echo $1 is down
ping通则显示up 否则显示down
[root@foundation77 ost]# chmod +x check_ip.sh
[root@foundation77 ost]# /ost/check_ip.sh 172.25.254.250
172.25.254.250 is up
[root@foundation77 ost]# /ost/check_ip.sh 172.25.22.40
172.25.254.40 is down
编辑shell脚本一般以.sh结尾
[root@foundation77 ~]# cd /ost
[root@foundation77 ost]# ls
[root@foundation77 ost]# vim westos.sh 编辑一个shell脚本
1 #!/bin/bash (幻数 shell脚本开头必须要有)
2 echo hello westos 保存退出
1.[root@foundation77 ost]# sh westos.sh
hello westos
2.[root@foundation77 ost]# chmod +x westos.sh 添加一个可执行权限
[root@foundation77 ost]# /mnt/westos.sh
hello westos
以上两种方式都可以看到shell脚本执行的结果
如果要看他的命令及结果,也有两种方式
1.[root@foundation77 ost]# sh -x /mnt/westos.sh
+ echo hello westos (执行的命令)
hello westos (执行的结果)
2.[root@foundation77 ost]# vim westos.sh
1 #!/bin/bash -x (最后加入-x)
2 echo hello westos 保存退出
[root@foundation77 ost]# /mnt/westos.sh
+ echo hello westos
hello westos (此时可以查看到命令及结果)
![](https://i-blog.csdnimg.cn/blog_migrate/2d9a7edacac582f8e16447b8ef04aeae.png)
一般来说,脚本需要有作者,说明及创建时间等,如果每次编写则太过麻烦,可以写在系统文件中自动读取 有两种编辑方式
1.(第一种方法)
[root@foundation77 ost]# vim /etc/vimrc 编辑此文件
67 map <F4> ms:call WESTOS()<cr>'s (编辑文件按F4自动执行WESTOS命令)
68 function WESTOS() (68及以下为命令内容)
69 call append(0,"#####################################")
70 call append(1,"# Author : lee #")
71 call append(2,"# Mail : lee@westos.com #")
72 call append(3,"# Version : 1.0 #")
73 call append(4,"# Create_Time : ".strftime("%Y-%m-%d")." #")
74 call append(5,"# Description : #")
75 call append(6,"# #")
76 call append(7,"# #")
77 call append(8,"#####################################")
78 call append(9,"")
79 call append(10,"#!/bin/bash")
80 endfunction
编辑完成后保存退出
1 #####################################
2 # Author : lee #
3 # Mail : lee@westos.com #
4 # Version : 1.0 #
5 # Create_Time : 2018-05-09 #
6 # Description : #
7 # #
8 # #
9 #####################################
10
11 #!/bin/bash
12 #!/bin/bash -x
13 echo hello westos 按F4后会出现刚才编辑的表头
![](https://i-blog.csdnimg.cn/blog_migrate/616da4265661176229a1f11cb7f04415.png)
2.第二种方法
[root@foundation77 ost]# vim /etc/vimrc
67 autocmd BufNewFile *.sh exec ":call WESTOS()" (当编辑一个新的以.sh结尾的shell时,会出现WESTOS表头)
68 "map <F4> ms:call WESTOS()<cr>'s (使用"注释掉本行命令)
69 function WESTOS()
70 call append(0,"#####################################")
71 call append(1,"# Author : lee #")
72 call append(2,"# Mail : lee@westos.com #")
73 call append(3,"# Version : 1.0 #")
74 call append(4,"# Create_Time : ".strftime("%Y-%m-%d")." #")
75 call append(5,"# Description : #")
76 call append(6,"# #")
77 call append(7,"# #")
78 call append(8,"#####################################")
79 call append(9,"")
80 call append(10,"#!/bin/bash")
81 endfunction 保存退出
[root@foundation77 ost]# vim westos.sh
1 #####################################
2 # Author : lee #
3 # Mail : lee@westos.com #
4 # Version : 1.0 #
5 # Create_Time : 2018-05-09 #
6 # Description : #
7 # #
8 # #
9 #####################################
10
11 #!/bin/bash
此时不需要按F4 会自动弹出表头 (必须编辑新的以.sh结尾的shell才可以,编辑以前存在的则不会出现)
执行ip_show.sh 显示当前主机的ip地址
[root@foundation77 ost]# vim ip_show.sh
12 ifconfig br0 | awk -F " " '/inet\>/{print $2}' 加入此行 保存退出
[root@foundation77 ost]# chmod +x ip_show.sh
[root@foundation77 ost]# sh ip_show.sh
172.25.22.77
[root@foundation77 ost]# vim user_show.sh
12 awk -F : '/bash$/{print $1}' /etc/passwd 加入此行 保存退出
[root@foundation77 ost]# chmod +x user_show.sh
[root@foundation77 ost]# /mnt/user_show.sh
root
kiosk
shell中的命令
[root@foundation77 ost]# vim westos
[root@foundation77 ost]# cat westos
123
[root@foundation77 ost]# vim westos1
[root@foundation77 ost]# cat westos1
123
456
[root@foundation77 ost]# diff westos westos1
1a2 (此行表示第一个文件的第二行中添加下面第二个文件中多出来的内容)
> 456 (第二个文件中多出来的内容)
diff在比较文件过程中的结果读取方式
[num1,num2][a|c|d][num3,num4]
num1,num2 表示在第一个文件中的行数
a表示添加-----add c表示更改-------change d表示删除-----delete
<表示第一个文件中的内容,>表示第二个文件中的内容
num3,num4表示在第二个文件中的行数
比较出不同可以用来做补丁
[root@foundation77 ost]# diff -u westos westos1 >file.path 把两个不同导入到file.path文件
下载patch服务
[root@foundation77 ost]# yum install patch -y
[root@foundation77 ost]# patch westos file.path 添加补丁
patching file westos
[root@foundation77 ost]# cat westos
12
[root@foundation77 ost]# cat westos1
12 此时两个文件一样
也可以备份原文件再添加补丁
[root@foundation77 ost]# patch -b westos file.path
patching file westos 备份原westos文件
[root@foundation77 ost]# ls
file.path westos westos1 westos.orig (备份文件)
cut
cut命令用于字符截取
cut -d 指定分隔符 cut -f 1,7|1-7 指定截取的列 cut -c 1,4|1-4 指定截取的字符位置
[root@foundation77 ost]# vim user_show.sh
grep bash /etc/passwd |cut -d : -f 1 指定分隔符为“:” 截取的列为第一列
[root@desktop22 mnt]# chmod +x user_show.sh
[root@foundation77 ost]# /ost/user_show.sh
root
kiosk 此时可以看到登陆系统的用户
12 ifconfig eth0 | head -n 2 | tail -n 1 | cut -d " " -f 10 保存退出
[root@foundation77 ost]# chmod +x ip_show.sh
[root@foundation77 ost]# /ost/ip_show.sh
172.25.22.10 此时可以看到ip
![](https://i-blog.csdnimg.cn/blog_migrate/b7a1fc3579b46e509285958a92ff8b2e.png)
sort 多用于字符排列
sort -n 纯数字排列
sort -r 倒叙
sort -u 去掉重复数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定要排序的列
[root@foundation77 ost]# ll
total 32
-rw-r--r--. 1 root root 119 Jun 14 10:44 file.path
-rwxr-xr-x. 1 root root 401 Jun 14 09:58 ip_show.sh
-rw-r--r--. 1 root root 411 Jun 14 11:16 ips_show.sh
-rwxr-xr-x. 1 root root 396 Jun 14 11:00 user_show.sh
-rwxr-xr-x. 1 root root 392 Jun 14 11:02 usr_show.sh
-rw-r--r--. 1 root root 4 Jun 14 10:15 westos
-rw-r--r--. 1 root root 8 Jun 14 10:15 westos1
-rw-r--r--. 1 root root 376 Jun 14 09:33 westos.sh
![](https://i-blog.csdnimg.cn/blog_migrate/f7581b3355c3c9c4437a214555e55032.png)
当前目录中的文件,按从大到小排序
[root@foundation77 ost]# ls -l /mnt | sort -t " " -k 5 -rn
-rw-r--r--. 1 root root 266 May 16 08:32 useradd.sh
-rwxr-xr-x. 1 root root 223 Apr 21 15:43 vm_create.sh
-rw-r--r--. 1 root root 18 May 16 08:18 userfile
-rw-r--r--. 1 root root 12 May 16 08:18 passwdfile
total 16
先列出文件做为后面的输出项,-t指定分隔符为“空格” 然后指定要排序的列为第五列,然后倒序排出
uniq 对重复字符做相应的处理 (搭配sort使用)
uniq -u 显示唯一的行
uniq -d 显示重复的行
uniq -c 每行显示一次并统计重复次数
[root@foundation77 ost]# cat westos3
1
2
4
8
6
5
9
6
7
3
4
7
8
5
11
![](https://i-blog.csdnimg.cn/blog_migrate/a753459cd51f83e3aad5191e489ef9d5.png)
[root@foundation77 ost]# sort -n westos3 | uniq -d 显示重复的行排列
4
5
6
7
8
[root@foundation77 ost]# sort -n westos3 | uniq -c 每行显示一次并统计重复次数
1 1
1 2
1 3
2 4
2 5
2 6
2 7
2 8
1 9
1 11
[root@foundation77 ost]# sort -n westos3 | uniq -u 显示唯一的行并排序
1
2
3
9
11
&&和||
&&用来执行条件成立后执行的命令 ||用来执行条件不成立后执行的命令
[root@foundation77 ost]# vim check_ip.sh
12 ping -c1 -w1 $1 &>/dev/null && echo $1 is up || echo $1 is down
ping通则显示up 否则显示down
[root@foundation77 ost]# chmod +x check_ip.sh
[root@foundation77 ost]# /ost/check_ip.sh 172.25.254.250
172.25.254.250 is up
[root@foundation77 ost]# /ost/check_ip.sh 172.25.22.40
172.25.254.40 is down
![](https://i-blog.csdnimg.cn/blog_migrate/c8d1025959e35cae9a81b18608cae10a.png)
$1:表示输入的所有命令