一、什么是shell和shell脚本
1、什么是shell
Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行
2、什么是shell脚本
脚本是一种解释型语言。用shell脚本保存执行动作;用脚本判定命令的执行条件;用脚本来实现动作的批量执行。
shell有两种执行方式
•交互式(Interactive):解释执行用户的命令,用户输入一条命令,Shell就解释执行一条。
•批处理(Batch):用户事先写一个Shell脚本(Script),其中有很多条命令,让Shell一次把这些命令执行完,而不必一条一条地敲命令。
二、创建shell脚本(标题信息)
[root@localhost ~]# vim /etc/vimrc
#编辑/etc/vimrc文件
66 map <F4> ms:call WESTOS()<cr>'s
#按F4时会调用下面的信息
67 func WESTOS()
68 call append(0,"#########################")
69 call append(1,"# author: lucy".(" #"))
70 call append(2,"# Version: ")
71 call append(3,"# Mail: ")
72 call append(4,"# Date: ".strftime("%Y-%m-%d"))
73 call append(5,"# Description: ")
74 call append(6,"# ")
75 call append(7,"#########################")
76 call append(8," ")
77 call append(9,"#! /bin/bash")
78 endfunc
#AUTHOR 脚本作者
#VERSION 脚本的版本
#MAIL 脚本作者联系方式
#DATE 脚本创作时间
#DESCRIPTION 描述信息
#!/bin/bash 脚本使用的解释器,通常用幻数 "#!" 指定
示例
(1)编辑/vim/vimrc配置文件
如果继续添加下面的信息,那么只要是以.sh结尾的文件,就会自动添加上面的标题信息
67 autocmd BufNewFile *.sh exec ":call W ESTOS()"
创建以.sh结尾文件
三、diff命令
diff是用来 比较两个比较两个文件或目录的不同
diff file1 file2
diff -r directory1 directory2
[num1,num2]a|c|d[num3,num4]
num1,num2表示在第一个文件第几行
a表示添加:add
c表示更改:change
d表示删除:delete
num3,num4 表示在第二个文件中第几行数
[root@localhost mnt]# vim westos
#编辑westos文件
[root@localhost mnt]# vim westos1
#编辑westos1文件
[root@localhost mnt]# cat westos
#查看westos文件的内容
hello world
[root@localhost mnt]# cat westos1
#查看westos1的文件内容
hello world
linux
[root@localhost mnt]# vim westos1
#删除westos1文件中第二行的linux
#那么westos文件和westos1文件的内容完全相同,
#通过用diff比较两个文件的不同时,没有 任何信息的提示
[root@localhost mnt]# diff westos westos1
#比较两个文件的不同
[root@localhost mnt]# vim westos1
#编辑westos1的文件内容,westos文件内容保持不变
[root@localhost mnt]# cat westos1
#查看westos1文件的内容
hello lucy
[root@localhost mnt]# cat westos
#查看westos文件的内容
hello world
比较两个文件的不同,由下图可以看出,1c1的意思是只要将第一个文件的第一行的信息进行修改,那么就会得到跟westos1文件相同的内容
[root@localhost mnt]# cat westos
#编辑完westos文件并查看文件的内容
hello world
sweet dream
[root@localhost mnt]# cat westos1
#编辑westos1文件并查看文件的内容
hello world
由下图可以看出。如果删除第一个文件的第二行信息,就会得到与westos1文件完全相同的内容
打补丁
[root@localhost mnt]# yum install patch -y #安装patch安装包
[root@localhost mnt]# diff -u westos westos1 > westos.path
#生成补丁文件
[root@localhost mnt]# patch westos westos.path
# 打补丁(会覆盖源文件内容)
[root@localhost mnt]# ls
westos westos1 westos.path
[root@localhost mnt]# patch -b westos westos.path
#打补丁不会覆盖源件
patching file westos
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file westos.rej
[root@localhost mnt]# ls
#查看是否会由源文件的产生,由下图可以看出westos.orig是源文件,
#而westos.path后来生成的补丁文件,文件没有被覆盖
westos westos1 westos.orig westos.path
四、sort排序
sort -n 纯数字排列
sort -r 倒序
sort -u 去掉重复数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定排序的列
-urn是以纯数字,去掉重复数字进行倒序排序,并将排序 结果输出到westos文件中,-o 输出指定到某个文件中去
由下图我们
查看文件的内容,指定以:为分隔符,对第二列进行排序:sort -t : -k 2 排序的文件
五、cut用于字符截取
cut -d 指定分隔符
cut -f 1,7|1-7 指定截取的列
cut -c 1,4|1-4 指定截取的字符位置
[root@localhost mnt]# vim ip_show.sh
#编辑ip显示文件,注意这个文件只要是以.sh结尾就好
#!/bin/bash
ifconfig eth0 | head -n 2 | tail -n 1 > /mnt/test
cut -d " " -f 10-11 /mnt/test
#将ifconfig eth0的信息输出第二行并且将结果输出到/mnt/test文件中,然后截取/mnt/test文件中的以空格为分隔符的第10到11行字符
#ifconfig eth0 查看eth0设备信息
#head -n 2 输出前两行信息
#tail -n 1 输出最后一行的信息
#> /mnt/test 将结果输出到/mnt/test文件
#cut -d " " 以指定的分隔符进行截取,这里的分隔符是空格的形式
#-f 指定截取的列
[root@localhost mnt]# sh ip_show.sh
#执行脚本,由下面的信息可以看出,只截取ip信息成功
172.25.254.218
六、uniq 对重复字符做相应的处理,uniq配合sort用
uniq 对重复字符做相应的处理
uniq -u 显示唯一的行
uniq -d 显示重复的行
uniq -c 每行显示一次并统计重复次数
示例
编辑file文件内容
使用uniq -u显示唯一的一行,结合sort -n 是使用,对file文件进行按纯数字的形式进行排序,并显示没有重复出现的 一行
对file文件进行纯数字排序,并显示重复的一行
对file文件的内容进行纯数字排序,每行显示一次并统计重复次数
七、5. &&和|| 是与非。
&& 用来执行条件成立后执行命令。
|| 用来执行条件不成立后执行命令。
[root@localhost mnt]# vim check_ip.sh
#编辑check_ip.sh文件
########################
# author: lucy #
# Version:
# Mail:
# Date: 2018-06-10
# Description:
#
########################
#!/bin/bash
ping -c1 -w1 $1 &> /dev/null && echo $1 is up || echo $1 is down
#编辑脚本ping “IP”通的显示 "IP" is up不通显示"IP" is down
[root@localhost mnt]# sh check_ip.sh 172.25.254.218
#执行脚本,由下面的信息可以看出,172.25.254.218这台主机网络是通畅的
172.25.254.218 is up
八、test 和[ ]等同
test "$A"="$B" 等同 ["$A"="$B"]
["$A"="$B"] 表示"$A"="$B"成立
["$A"!="$B"] 表示"$A"="$B"不成立
[ -z "$A"] 表示$A是否为空
[ -n "$A"] 表示$A是否不为空
示例
赋值a=2,b=3
判断a和b是否相等,若相等,则输出yes,否则输出no
判断a和b是否不相等,若不相等,则输出yes,否则输出no
判断a是否为空,如果为空,输出yes,否则输出为no
判断a是否不为空,如果为不为空,输出yes,否则输出为no
整型数据比较:
参数:
-eq 等于
-ne 不等于
-le 小于等于
-lt 小于
-ge 大于等于
-gt 大于
-o 表示或者
-a 表示并且
直接使用命令练习test和[ ]命令
[root@localhost mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
#判断a和b是否相等,若相等输出yes,否则输出为no
no
[root@localhost mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
#判断a和b是否不相等,若不相等输出yes,否则输出为no
yes
[root@localhost mnt]# [ "$a" -le "$b" ] && echo yes || echo no
#判断a是否小于相等b,若小于等于b输出yes,否则输出为no
yes
[root@localhost mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
#判断a是否小于b,若小于b输出yes,否则输出为no
yes
[root@localhost mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
#判断a是否大于相等b,若大于等于b输出yes,否则输出为no
no
[root@localhost mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
#判断a是否大于b,若大于b输出yes,否则输出为n
no
[root@localhost mnt]# [ "$a" = "$b" -o "$a" -lt 10 ] && echo yes || echo no
#若a和b相等或者a<10,则输出为yes,否则输出为0
yes
[root@localhost mnt]# [ "$a" = "$b" -a "$a" -lt 10 ] && echo yes || echo no
#若a和b相等并且a<10,则输出yes,否则输出为0
no
脚本的形式练习test和[]命令
示例:
如果给定的数值大于0并且小于10,如果满足条件显示此数值在1到10之间,如果不满足,则输出为不在1到10之间,如果没有给值,那么系统会提示相关信息,请求先赋予数值
[root@localhost mnt]# vim num_check.sh
#编辑脚本文件
########################
# Author: lucy #
# Version:
# Mail:
# Date: 2018-06-10
# Description:
#
########################
#!/bin/bash
test -z "$1" &&{
echo please give me a number after script!!
exit 1
}
test "$1" -gt 0 -a "$1" -lt "10" &&{
echo "$1 is between 1 -10"
}|| {
echo "$1 is not between 1-10"
}
[root@localhost mnt]# sh num_check.sh
#因为执行脚本时后面没有跟任何数值,那么,系统输出请给一个数
please give me a number after script!!
[root@localhost mnt]# sh num_check.sh 13
#给定一个数值13,不符合条件,所以输出此值不在1到10范围内
13 is not between 1-10
[root@localhost mnt]# sh num_check.sh 2
#给定一个数值2,符合条件,所以输出此值在1到10范围内
2 is between 1 -10
test判断两个文件
[“文件名” -ef “文件名”] #判断两个文件的节点是否一致
[“文件名” -ot “文件名”] #判断两个文件哪个更早建立
[“文件名” -ot “文件名”] #判断两个文件哪个更晚建立
(1)判断两个文件的节点是否一致
[root@localhost mnt]# touch file
#创建file文件
[root@localhost mnt]# ln /mnt/file /mnt/file1
#建立硬链接文件
[root@localhost mnt]# ls -li *
#查看/mnt下文件的节点
11752745 -rw-r--r--. 2 root root 0 Jun 10 02:53 file
11752745 -rw-r--r--. 2 root root 0 Jun 10 02:53 file1
[root@localhost mnt]# [ "/mnt/file" -ef "/mnt/file1" ]&& echo yes || echo no
#判断file文件和file1文件的节点是否一致,如果一致,输出yes,否则输出no
yes
[root@localhost mnt]# [ "/mnt/file" -ef "/etc/passwd" ]&& echo yes || echo no
#判断file与passwd文件的节点是否一致,如果一致,输出为yes,否则输出为n0
(2)判断两个文件,哪个更早建立
[root@localhost mnt]# rm -fr file1
#删除上面例子建立的file1文件
[root@localhost mnt]# touch file1
#创建file1文件
[root@localhost mnt]# [ "file" -ot "file1" ]&& echo yes || echo no
#判断file文件是否比file1文件更早建立,如果是,输出为yes,否则输出为no
yes
(3)判断两个文件,哪个更晚建立
[root@localhost mnt]# [ "file" -nt "file1" ]&& echo yes || echo no
#判断file文件是否比file1文件更晚建立,如果是输出yes,否则输出为no
no
test命令:判定文件的类型
参数:
[ -e "file" ] 文件是否存在
[ -f "file" ] 文件是否为普通文件
[ -L "file" ] 文件时否为连接(软连接)
[ -S "file" ] 文件是否为套接字
[ -b "file" ] 文件是否为块设备
[ -d "file" ] 文件是否为目录
[ -c "file" ] 文件是否为字符设备
ln -S /mnt/file /mnt/hello 创建链接-s表示软链接,不加-s表示硬链接
示例
安装数据库
打开数据库
编辑脚本文件
创建软链接
判断文件是否套接子
判断文件是否为块设备
判断文件是否为一个字符设备
创建目录,并判断quer是否为一个目录
脚本形式显现
[root@localhost mnt]# vim sweet.sh
########################
# Author: lucy #
# Version:
# Mail:
# Date: 2018-06-10
# Description:
#
########################
#!/bin/bash
[ -z "$1" ] && { # 脚本后是否为空
echo "please give me a file!!" #为空输出please give me a file
exit 1 #退出
}
[ -e "$1" ] || { #文件是否存在
echo "$1 is not exist!!" #显示文件不存在
exit 0
}
[ -L "$1" ] && { #文件是否为链接
echo "$1 is a link" #如果是输出此文件为链接
exit 0
}
[ -f "$1" ] && { #文件是否为普通文件
echo "$1 is a common file" #文件是普通文件
exit 0
}
[ -b "$1" ] &&{ #该文件是否为块设备
echo "$1 is a block file" #输出文件是块设备
exit 1
}
[root@localhost mnt]# sh sweet.sh
#执行命令时,没有跟任何文件,输出请给我一个文件的提示信息
please give me a file!!
[root@localhost mnt]# sh sweet.sh /dev/vdb
#/dev/vdb是一个块设备
/dev/vdb is a block file
[root@localhost mnt]# sh sweet.sh file
#file文件是一个普通的文件
file is not exist!!
[root@localhost mnt]# sh sweet.sh /etc/system-release
#该文件是一个链接
/etc/system-release is a link
九、tr大小写转换
tr ‘a-z’ ‘A-Z’ < /mnt/westos > /mnt/hello
将westox内容转化成大写并重定向到/mnt/hello中
通常我们在进行一些命令时,都有大小写之分,为了统一大小写,需要进行大小写的转换
[root@localhost mnt]# vim test.sh
########################
# Author: lucy #
# Version:
# Mail:
# Date: 2018-06-10
# Description:
#
########################
#!/bin/bash
random=$(echo $1 | tr 'A-Z' 'a-z')
[ "$random" = "hello" ]&& {
echo yes
}||{
echo no
}
[root@localhost mnt]# sh test.sh HELLO
yes
[root@localhost mnt]# sh test.sh hello
yes