一、脚本概述
Shell就是一个命令行解释器,它的作用就是遵循一定的语法将输入的命令加以解释并传给系统。它为用户提供了一个向Linux发送请求以便运行程序的接口系统级程序,用户可以用Shell来启动、挂起、停止甚至是编写一些程序。 Shell本身是一个用C语言编写的程序,它是用户使用Linux的桥梁。Shell既是一种命令语言,又是一种程序设计语言。
1.shell脚本–> 命令行解析器
是一个文件,命令的有序集合
脚本文件以“.sh”结尾
2.脚本编写
#!/bin/bash ##执行脚本的shell类型
3.脚本运行
sh hello.sh ##用sh打开脚本
chmod +x hello.sh ##添加执行权限
/mnt/hello.sh ##用脚本本身的shell打开
[root@host1 mnt]# vim hello.sh
[root@host1 mnt]# sh hello.sh
hello world
[root@host1 mnt]# chmod +x hello.sh
[root@host1 mnt]# /mnt/hello.sh
hello world
4.脚本调试
[root@host1 mnt]# sh -x hello.sh
+ echo hello world
hello world
[root@host1 mnt]#
5.自动添加脚本信息说明 ##配置文件/etc/vimrc
66 autocmd BufNewFile *.sh exec “:call WESTOS( )”
67 ##新建的以.sh结尾的文件自动添加函数WESTOS
68 ” map ms: call WESTOS( )’s
69 ##按F4自动添加函数WESTOS,“ 表示注释
67 autocmd BufNewFile *.sh exec ":call WESTOS()"
68 "map <F4> ms:call WESTOS()<cr>'s
69 function WESTOS()
70 call append(0,"##################################################")
71 call append(1,"# Auther : mark #")
72 call append(2,"# Mail : mark.@163.com #")
73 call append(3,"# Version : #")
74 call append(4,"# Create_time : ".strftime("%Y-%m-%d")." #")
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@host1 mnt]# vim file.sh
1 ##################################################
2 # Auther : mark #
3 # Mail : mark.@163.com #
4 # Version : #
5 # Create_time : 2018-05-12 #
6 # Description : #
7 # #
8 # #
9 ##################################################
10
11 #!/bin/bash
12
6.shell脚本变量命名规则:
- 见名知义
- 由字母、下划线、数字组成
- —-不能以数字开头——
7.变量赋值:
var=5;
注意:在变量赋值的时候等号两边不能有空格
8.变量的引用(输出):
使用echo:
echo $var
注意:在变量的输出时要加($)符号
9.内置环境变量:
PATH HOME USER HOSTTYPE
10.内置位置变量:
1.$0 : shell脚本文件名
2.$1-$9 : 命令行传入的参数,从脚本文件名后一个参数开始
3.$* : 命令行传入的所有参数,除了脚本文件名
4.$@ : 命令行传入的所有参数,除了脚本文件名
5.$$ : shell脚本的进程号
二、简单脚本练习 ##脚本添加x权限
1.运行脚本 ip_show.sh 显示当前主机ip
1 ##################################################
2 # Auther : mark #
3 # Mail : mark.@163.com #
4 # Version : #
5 # Create_time : 2018-05-12 #
6 # Description : #
7 # #
8 # #
9 ##################################################
10
11 #!/bin/bash
12 ifconfig eth0 | awk -F " " '/inet /{print $2}'
[root@host1 mnt]# vim ip_show.sh
[root@host1 mnt]# chmod +x ip_show.sh
[root@host1 mnt]# /mnt/ip_show.sh
172.25.254.1
11 #!/bin/bash
12 ip addr show eth0 | awk -F " " '/inet /{print $2}'
[root@host1 mnt]# vim ip_show.sh
[root@host1 mnt]# /mnt/ip_show.sh
172.25.254.1/24
2.运行脚本 user_show.sh 显示当前主机可登陆的用户
vim /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 chrony:x:998:996::/var/lib/chrony:/sbin/nologin
11 student:x:1000:1000:Student User:/home/student:/bin/bash
12 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
1 ##################################################
2 # Auther : mark #
3 # Mail : mark.@163.com #
4 # Version : #
5 # Create_time : 2018-05-12 #
6 # Description : #
7 # #
8 # #
9 ##################################################
10
11 #!/bin/bash
12 grep bash /etc/passwd | awk -F : '{print $1}'
[root@host1 mnt]# vim user_show.sh
[root@host1 mnt]# chmod +x user_show.sh
[root@host1 mnt]# /mnt/user_show.sh
root
student
3.运行脚本 clear_log.sh 清空当前日志
1 ##################################################
2 # Auther : mark #
3 # Mail : mark.@163.com #
4 # Version : #
5 # Create_time : 2018-05-12 #
6 # Description : #
7 # #
8 # #
9 ##################################################
10
11 #!/bin/bash
12 > /var/log/messages
[root@host1 mnt]# vim clear_log.sh
[root@host1 mnt]# chmod +x clear_log.sh
[root@host1 mnt]# /mnt/clear_log.sh
[root@host1 mnt]#
三、基本脚本命令
diff ##比较文件差异
1.对比两个文件的不同
[root@host1 mnt]# vim file1
[root@host1 mnt]# vim file2
[root@host1 mnt]# cat file1
wps
123
[root@host1 mnt]# cat file2
wps
[root@host1 mnt]# diff file1 file2
2d1
< 123
**file1删除第二行的123后和file2相同**
2.生成补丁
[root@host1 mnt]# diff file1 file2 -u > file.path
[root@host1 mnt]# cat file.
file.path file.sh
[root@host1 mnt]# cat file.path
--- file1 2018-05-12 14:32:42.940826819 +0800
+++ file2 2018-05-12 14:33:17.174827209 +0800
@@ -1,2 +1 @@
wps
-123
patch
1.安装补丁软件 patch.x86_64
2.打补丁
[root@host1 mnt]# cat file1
wps
123
[root@host1 mnt]# patch file1 file.path
patching file file1
[root@host1 mnt]# cat file1
wps
3.打补丁的同时对原文件备份 (.orig)
[root@host1 mnt]# patch file1 file.path -b
patching file file1
[root@host1 mnt]# cat file1
wps
[root@host1 mnt]# ls
clear_log.sh file1.orig file.path ifcfg-eth0 iptables user_show.sh
file1 file2 hello.sh ip_show.sh passwd
[root@host1 mnt]# cat file1.orig
wps
123
cut ##多用于截取字符
1.cut参数
cut -d ##指定分隔符
cut -f 1 ##指定截取每行第1列
cut -c 3 ##指定截取每行第3个字符
2.用cut编辑脚本 user_show.sh
1 ##################################################
2 # Auther : mark #
3 # Mail : mark.@163.com #
4 # Version : #
5 # Create_time : 2018-05-12 #
6 # Description : #
7 # #
8 # #
9 ##################################################
10
11 #!/bin/bash
12 grep bash /etc/passwd | cut -d : -f 1
[root@host1 mnt]# vim user_show.sh
[root@host1 mnt]# /mnt/user_show.sh
root
student
3.用cut编辑脚本 ip_show.sh
1 ##################################################
2 # Auther : mark #
3 # Mail : mark.@163.com #
4 # Version : #
5 # Create_time : 2018-05-12 #
6 # Description : #
7 # #
8 # #
9 ##################################################
10
11 #!/bin/bash
12 ip addr show eth0 | head -n 3 | tail -n 1 | cut -d " " -f 6
[root@host1 mnt]# vim ip_show.sh
[root@host1 mnt]# /mnt/ip_show.sh
172.25.254.1/24
sort ##多用于字符排序
1.sort参数
sort -n ##纯数字排序 sort -r ##倒序
sort -u ##去掉重复数字 sort -o ##输出到指定文件中
sort -t ##指定分割符 sort -k ##指定要排序的列
2.对mnt文件排序,找出最大的两个文件
[root@host1 mnt]# ll
total 48
-rwxr-xr-x. 1 root root 492 5月 12 14:27 clear_log.sh
-rw-r--r--. 1 root root 4 5月 12 15:25 file1
-rw-r--r--. 1 root root 8 5月 12 15:22 file1.orig
-rw-r--r--. 1 root root 4 5月 12 15:22 file2
-rw-r--r--. 1 root root 116 5月 12 15:23 file.path
-rwxr-xr-x. 1 root root 29 5月 12 10:28 hello.sh
-rw-r--r--. 1 root root 183 5月 10 20:36 ifcfg-eth0
-rwxr-xr-x. 1 root root 532 5月 12 15:33 ip_show.sh
-rw-------. 1 root root 4913 5月 11 22:29 iptables
-rw-r--r--. 1 root root 492 5月 12 13:38 passwd
-rwxr-xr-x. 1 root root 510 5月 12 15:30 user_show.sh
[root@host1 mnt]# ll | sort -t " " -k 5 -rn | head -n 2
-rw-------. 1 root root 4913 5月 11 22:29 iptables
-rwxr-xr-x. 1 root root 532 5月 12 15:33 ip_show.sh
[root@host1 mnt]#
uniq ##对重复字符的处理
1.uniq参数
uniq -u ##显示唯一的行
uniq -d ##显示重复的行
uniq -c ##每行显示的次数
注意:配合sort命令使用
2.编辑文件,对重复字符处理
[root@host1 mnt]# vim test
[root@host1 mnt]# cat test
1
1
5
2
34
7
1
8
6
3
5
2
3
[root@host1 mnt]# sort -n test | uniq -u
6
7
8
34
[root@host1 mnt]# sort -n test | uniq -d
1
2
3
5
[root@host1 mnt]# sort -n test | uniq -c
3 1
2 2
2 3
2 5
1 6
1 7
1 8
1 34
&&和||
1.&& 用来执行条件成立后执行的命令
2.|| 用来执行条件不成立后执行的命令
3.显示ping其他主机的结果
1 ##################################################
2 # Auther : mark #
3 # Mail : mark.@163.com #
4 # Version : #
5 # Create_time : 2018-05-12 #
6 # Description : #
7 # #
8 # #
9 ##################################################
10
11 #!/bin/bash
12 ping -c1 -w1 $1 &> /dev/null && echo $1 is up || echo $1 is down
ping其他主机结果
[root@host1 mnt]# vim check_ip.sh
[root@host1 mnt]# chmod +x check_ip.sh
[root@host1 mnt]# /mnt/check_ip.sh 172.25.254.1
172.25.254.1 is up
[root@host1 mnt]# /mnt/check_ip.sh 172.25.254.3
172.25.254.3 is down
[root@host1 mnt]# /mnt/check_ip.sh
is down
**当执行脚本并没有加IP地址时,没有报错**
test ##判断条件是否成立
1. test 参数
test 等同于 [ ]
字符型数据
[root@host1 mnt]# a=1
[root@host1 mnt]# b=2
[root@host1 mnt]# [ "$a" = "$b" ] && echo yes || echo no
no
[root@host1 mnt]# [ "$a" != "$b" ] && echo yes || echo no
yes
[root@host1 mnt]# [ ! "$a" = "$b" ] && echo yes || echo no
yes
[root@host1 mnt]# b=1
[root@host1 mnt]# [ ! "$a" = "$b" ] && echo yes || echo no
no
整型数据
[root@host1 mnt]# a=10
[root@host1 mnt]# b=11
[root@host1 mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
no
[root@host1 mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
yes
[root@host1 mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
yes
[root@host1 mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@host1 mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
no
[root@host1 mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
no
[root@host1 mnt]# a=99
[root@host1 mnt]# b=101
[root@host1 mnt]# [ "$a"="$b" -o "$a" -lt "100" ] && echo yes || echo no
yes
[root@host1 mnt]# [ "$a"="$b" -a "$a" -lt "100" ] && echo yes || echo no
yes
[root@host1 mnt]# [ "$a" -eq "$b" -o "$a" -lt "100" ] && echo yes || echo no
yes
[root@host1 mnt]# [ "$a" -eq "$b" -a "$a" -lt "100" ] && echo yes || echo no
no
[root@host1 mnt]# [ "$a" -eq "$b" -a "$b" -gt "100" ] && echo yes || echo no
no
[root@host1 mnt]# a=md
[root@host1 mnt]# [ -z "$a" ] && echo yes || echo no
no
[root@host1 mnt]# [ -n "$a" ] && echo yes || echo no
yes
文件
[ "file1" -ef "file2" ] ##节点是否一致,即文件是否一样
建立文件,查看节点,创建硬链接
[root@host1 mnt]# touch redtest
[root@host1 mnt]# ln /mnt/redtest /mnt/redtest1
[root@host1 mnt]# ls -i /mnt/redtest /mnt/redtest1
9416153 /mnt/redtest 9416153 /mnt/redtest1
[root@host1 mnt]# [ "/mnt/redtest" -ef "/mnt/redtest1" ] && echo yes || echo no
yes
[ "file1" -nt "file2" ] ##file1是否比file2时间new
[ "file1" -ot "file2" ] ##file1是否比file2时间out
[ -e "file" ] ##文件是否存在
[ -f "file" ] ##是否是普通文件
[ -L "file" ] ##是否是软链接文件
[ -S "file" ] ##是否是套接字
[ -b "file" ] ##是否是块设备
[ -d "file" ] ##是否是目录
[ -c "file" ] ##是否是字符文件
2.编写脚本check_num.sh判断数字是否在0-10之间
1 #!/bin/bash
2 [ $1 -gt "0" -a $1 -lt "10" ] && echo yes || echo no
[root@host1 mnt]# vim check_num.sh
[root@host1 mnt]# chmod +x check_num.sh
[root@host1 mnt]# /mnt/check_num.sh 5
yes
[root@host1 mnt]# /mnt/check_num.sh 0
no
[root@host1 mnt]# /mnt/check_num.sh 10
no
3.编写脚本clean_log.sh,当超级用户时,情况日志,当普通用户,提示请切换用户
#!/bin/bash
12 User_ID=`id -u`
13 [ "$User_ID" -eq "0" ] && `> /var/log/messages` || echo 您不是超级用户!!
[root@host1 mnt]# vim clear_log.sh
[root@host1 mnt]# chmod +x clear_log.sh
[root@host1 mnt]# /mnt/clear_log.sh
[root@host1 mnt]# su - student
[student@host1 ~]$ /mnt/clear_log.sh
您不是超级用户!!
4.编写脚本check_file.sh判断文件类型
1 #!/bin/bash
2 [ -z "$1" ] && {
3 echo please give a filname
4 exit 1
5 }
6 [ -e "$1" ] || echo $1 is not exit
7 [ -L "$1" ] && {
echo $1 is a link file
exit 1
}
8 [ -f "$1" ] && echo $1 is a common file
9 [ -S "$1" ] && echo $1 is a sock file
10 [ -b "$1" ] && echo $1 is a block
11 [ -d "$1" ] && echo $1 is a directory
12 [ -c "$1" ] && echo $1 is a char file
[root@host1 mnt]# vim check_file.sh
[root@host1 mnt]# chmod +x check_file.sh
[root@host1 mnt]# /mnt/check_file.sh
please give a filname
[root@host1 mnt]# /mnt/check_file.sh /dev/vdb
/dev/vdb is a block
[root@host1 mnt]# /mnt/check_file.sh /mnt/redtest1
/mnt/redtest1 is a common file
[root@host1 mnt]# /mnt/check_file.sh /mnt/haha
/mnt/haha is not exit
[root@host1 mnt]# /mnt/check_file.sh /mnt
/mnt is a directory
[root@host1 mnt]# /mnt/check_file.sh /dev/pts/0
/dev/pts/0 is a char file
[root@host1 mnt]# ln -s redtest redtest2
[root@host1 mnt]# /mnt/check_file.sh /mnt/redtest2
/mnt/redtest2 is a link file
lrwxrwxrwx. 1 root root 7 5月 12 17:17 redtest2 -> redtest
tr ##转换字符
[root@host1 mnt]# vim file
[root@host1 mnt]# cat file
1224423422
westos
[root@host1 mnt]# tr '1-3' 'a-c' < /mnt/file
abb44bc4bb
westos
[root@host1 mnt]# tr 'a-z' 'A-Z' < /mnt/file
1224423422
WESTOS
[root@host1 mnt]# tr '1-3' 'a' < /mnt/file
aaa44aa4aa
westos
[root@host1 mnt]# tr '2' 'C' < /mnt/file
1CC44C34CC
westos
[root@host1 mnt]# cat file
1224423422
westos
**不改变原文件**
find ##查找文件
1.find 参数
-group ##查找组
-type ##查找类型
-user ##查找用户
-not -group ##查找不是该组的文件
-a | -o ## and | or
[root@host1 mnt]# ll
total 68
-rwxr-xr-x. 1 root root 328 May 12 17:08 check_file.sh
-rwxr-xr-x. 1 root root 537 May 12 15:59 check_ip.sh
-rwxr-xr-x. 1 root root 65 May 12 16:46 check_num.sh
-rwxr-xr-x. 1 root root 572 May 12 16:52 clear_log.sh
-rw-r--r--. 1 root student 18 May 12 17:27 file
lrwxrwxrwx. 1 root root 9 May 12 17:14 file01 -> /mnt/file
-rw-r--r--. 1 root root 4 May 12 15:25 file1
-rw-r--r--. 1 root root 8 May 12 15:22 file1.orig
-rw-r--r--. 1 student root 4 May 12 15:22 file2
-rw-r--r--. 1 root root 116 May 12 15:23 file.path
-rwxr-xr-x. 1 root root 29 May 12 10:28 hello.sh
-rw-r--r--. 1 root root 183 May 10 20:36 ifcfg-eth0
-rwxr-xr-x. 1 root root 532 May 12 15:33 ip_show.sh
-rw-------. 1 root root 4913 May 11 22:29 iptables
-rw-r--r--. 1 root root 492 May 12 13:38 passwd
-rw-r--r--. 2 root student 0 May 12 16:34 redtest
-rw-r--r--. 2 root student 0 May 12 16:34 redtest1
lrwxrwxrwx. 1 root root 7 May 12 17:17 redtest2 -> redtest
-rw-r--r--. 1 student root 27 May 12 15:51 test
-rwxr-xr-x. 1 root root 510 May 12 15:30 user_show.sh
[root@host1 mnt]# find /mnt/ -user student
/mnt/file2
/mnt/test
[root@host1 mnt]# find /mnt/ -not -group root
/mnt/redtest
/mnt/redtest1
/mnt/file
[root@host1 mnt]# find /mnt/ -group student
/mnt/redtest
/mnt/redtest1
/mnt/file
[root@host1 mnt]# find /mnt/ -type l
/mnt/file01
/mnt/redtest2
[root@host1 mnt]# find /mnt/ -user student -a -group root
/mnt/file2
/mnt/test
-perm 777 ##精确匹配权限775的文件
-perm -044 ##匹配u=0、g=4、o=4的文件
-perm /666 ##匹配u=6或g=6或o=6的文件
**u,g,o任一位都存在0权限**
[root@host1 mnt]# touch file{1..5}
[root@host1 mnt]# ls
file1 file2 file3 file4 file5
[root@host1 mnt]# chmod 775 file1
[root@host1 mnt]# chmod 644 file2
[root@host1 mnt]# chmod 444 file3
[root@host1 mnt]# chmod 044 file4
[root@host1 mnt]# chmod 004 file5
[root@host1 mnt]# ll
total 0
-rwxrwxr-x. 1 root root 0 5月 12 22:32 file1
-rw-r--r--. 1 root root 0 5月 12 22:32 file2
-r--r--r--. 1 root root 0 5月 12 22:32 file3
----r--r--. 1 root root 0 5月 12 22:32 file4
-------r--. 1 root root 0 5月 12 22:32 file5
[root@host1 mnt]# find /mnt/ -perm 775
/mnt/file1
[root@host1 mnt]# find /mnt/ -perm 044
/mnt/file4
[root@host1 mnt]# find /mnt/ -perm -044
/mnt/
/mnt/file1
/mnt/file2
/mnt/file3
/mnt/file4
[root@host1 mnt]# find /mnt/ -perm /044
/mnt/
/mnt/file1
/mnt/file2
/mnt/file3
/mnt/file4
/mnt/file5
-size 20k ##查找文件大小为20k的文件
-size -20k ##查找文件大小为小于20k的文件
-size +20k ##查找文件大小为大于20k的文件
[root@host1 mnt]# find /mnt/ -size 20k
/mnt/file2
[root@host1 mnt]# find /mnt/ -size -20k
/mnt/
/mnt/file3
/mnt/file4
/mnt/file5
[root@host1 mnt]# find /mnt/ -size +20k
/mnt/file1
**-maxdepth 1 -name passwd ##查找目录的深度为1层
-mindepth 2 -maxdepth 2 -name passwd ##只查找目录的第2层**
注意:最大深度为2层
[root@host1 mnt]# find /etc/ -name passwd
/etc/passwd
/etc/pam.d/passwd
[root@host1 mnt]# find /etc/ -maxdepth 1 -name passwd
/etc/passwd
[root@host1 mnt]# find /etc/ -maxdepth 2 -name passwd
/etc/passwd
/etc/pam.d/passwd
[root@host1 mnt]# find /etc/ -mixdepth 1 -name passwd
find: unknown predicate `-mixdepth'
[root@host1 mnt]# find /etc/ -mindepth 1 -name passwd
/etc/passwd
/etc/pam.d/passwd
[root@host1 mnt]# find /etc/ -mindepth 1 -maxdepth 1 -name passwd
/etc/passwd
[root@host1 mnt]# find /etc/ -mindepth 2 -maxdepth 2 -name passwd
/etc/pam.d/passwd
[root@host1 mnt]#