1.什么是shell
shell是操作系统中的软件,它用来保护内核并提供命令行
shell是解释性语言,相当于解释器,用shell脚本保存执行动作,用脚本判定命令的执行条件,实现动作的批量执行
shell脚本文件一般以.sh结尾
当文件没有执行权限时用sh调用
有可执行权限时可以用绝对路径直接调用
#!/bin/bash #幻数,指定解释器
#!/usr/bin/env bash #自动匹配解释器
脚本示例,如:
执行ip_show.sh #显示当前主机的ip地址
[root@localhost mnt]# vim ip_show.sh #编写脚本
#!/bin/bash
ifconfig
[root@localhost mnt]# sh ip_show.sh #调用脚本
/mnt/ip_show.sh内容
执行user_show.sh #显示当前主机中能登录系统的用户
[root@localhost mnt]# vim user_show.sh
#!/bin/bash
grep bash$ /etc/passwd
[root@localhost mnt]# sh user_show.sh
/mnt/user_show.sh内容:
2.在脚本文件里加注释
可编辑vim的配置文件/etc/vimrc
[root@localhost mnt]# vim /etc/vimrc
map <Tab> ms:call LINUX()<CR>'s #按Tab运行
或
autocmd BufNewFile *.sh exec ":call LINUX()" #新建的文件自动运行
下面为要加的注释内容
func LINUX ()
call append(0,"###############################")
call append(1,"#Author: lin".(" #"))
call append(2,"#Version: ".(" #"))
call append(3,"#Mail: ".(" #"))
call append(4,"#Date: ".strftime("%Y-%m-%d").(" #"))
call append(5,"#Description: ".(" #"))
call append(6,"# ".(" #"))
call append(7,"###############################")
call append(8,"")
call append(9,"#!/bin/bash")
endfunc
[root@localhost mnt]# vim file.sh 运行后—————>
###############################
#Author: lin #
#Version: #
#Mail: #
#Date: 2018-06-09 #
#Description: #
# #
###############################
#!/bin/bash
/etc/vimrc内容:
vim /mnt/file.sh
按Tab运行
新建的文件自动运行
/etc/vimrc内容:
/mnt/file1.sh
3.diff 和 patch 命令
diff命令是用来比较两个文件或目录的不同
patch命令是用来打补丁
diff在比较文件中结果读取方式
[num1,num2][a|c|d][num3,num4]
num1,num2表示在第一个文件中的行数
num3,num4表示在第二个文件中的行数
a表示添加
c表示更改
d表示删除
<表示第一个文件中的内容,>表示第二个文件中的内容,- - -分割线
1)比较文件并打补丁
[root@localhost mnt]# yum install patch
[root@localhost mnt]# vim file
hello 2018
[root@localhost mnt]# vim file1
hello 2018
happy new year
[root@localhost mnt]# diff file file1 #比较两个文件
2c2 #表示更改第一个文件的第二行才能匹配到第二个文件中
<
---
> happy new year
[root@localhost mnt]# diff -u file file1 #生成补丁
--- file 2018-06-12 05:32:59.754496222 -0400
+++ file1 2018-06-12 05:33:38.656496222 -0400
@@ -1,2 +1,2 @@
hello 2018
-
+happy new year
[root@localhost mnt]# diff -u file file1 > file.path #把补丁导入文件
[root@localhost mnt]# patch file file.path #打补丁
patching file file
查看两个文件:
[root@localhost mnt]# cat file
hello 2018
happy new year
[root@localhost mnt]# cat file1
hello 2018
happy new year
如果需要保存原文件:
[root@localhost mnt]# vim file
hello 2018
[root@localhost mnt]# patch -b file file.path #打补丁并保存原文件
patching file file
[root@localhost mnt]# cat file
hello 2018
happy new year
[root@localhost mnt]# ls
file file1 file.orig file.path
[root@localhost mnt]# cat file.orig
hello 2018 #原文件被保存
vim /mnt/file
vim /mnt/file1
比较文件并打补丁
打补丁且保存原文件
2)比较目录
[root@localhost mnt]# mkdir test
[root@localhost mnt]# mkdir test1
[root@localhost mnt]# touch test/file
[root@localhost mnt]# diff -r test test1 #加-r,比较目录
Only in test: file
4.cut 命令
多用于字符截取
cut -d #指定分隔符
cut -f #指定截取的列,如cut -f 1,7截取第1列和第7列,cut -f 1-7截取第列到第7列
cut -c #指定截取的字符位置,如cut -c 1,6截取每行第一个字符和第6个字符
如:
[root@localhost mnt]# cut -d : -f 1 /etc/passwd #截取第一列字符
[root@localhost mnt]# cut -c 1-6 /etc/passwd #截取第1-6个字符
编写脚本,只显示本机ip
[root@localhost mnt]# vim ip_show.sh
#!/bin/bash
ifconfig eth0 | grep "inet " | cut -d " " -f 10
[root@localhost mnt]# sh ip_show.sh
172.25.254.152
截取第一列字符
截取第1-6个字符
编写脚本,只显示本机ip
/mnt/ip_show.sh内容:
5.&&和||
&& #用来执行条件成立后执行的命令
|| #用来执行条件不成立时执行的命令
示例:
[root@localhost mnt]# ping -c1 -w1 172.25.254.155 && echo up || echo no #172.25.254.155能ping通输出yes,否则输出no
[root@localhost mnt]# ping -c1 -w1 172.25.254.111 && echo up || echo no #172.25.254.111能ping通输出yes,否则输出no
6.sort 命令
多用于字符排序
sort -r #倒序
sort -n #纯数字排序
sort -u #去掉重复数字
sort -t #指定分割符
sort -k #指定要排序的列
sort -o #输出到指定文件中
示例:
[root@localhost mnt]# sort -r num #倒序
[root@localhost mnt]# sort -n num #纯数字排序
[root@localhost mnt]# sort -u num #去掉重复数字
[root@localhost mnt]# sort -t : -k 2 -n num #把num文件按:分割的第2列进行纯数字排序
创建文件
编辑文件
7.uniq 命令
dui重复的字符做出相应的处理
uniq -d #显示重复的行
uniq -u #显示唯一的行
uniq -c #每行显示一次并统计重复次数
示例:
[root@localhost mnt]# sort num | uniq -d #显示num文件中重复的行
[root@localhost mnt]# sort num | uniq -c #每行显示一次并统计重复次数
[root@localhost mnt]# sort num | uniq -u #显示唯一的行
8.test 命令
test 命令和[ ]等同
如test “file” -ef “file1” 等同于 [ “file” -ef “file1” ]
[ "$A" = "$B" ] #A等于B
[ "$A" != "$B" ] #A不等于B
[ "$A" -eq "$B" ] #AB字符相同
[ "$A" -ne "$B" ] #AB字符不相同
[ "$A" -le "$B" ] #A小于B
[ "$A" -lt "$B" ] #A小于等于B
[ "$A" -ge "$B" ] #A大于B
[ "$A" -gt "$B" ] #A大于等于B
[ "$A" -ne "$B" -a "$A" -gt "$B" ] #-a表示并且
[ "$A" -ne "$B" -o "$A" -gt "$B" ] #-o表示或者
[ -z "$A" ] #A是否为空
[ -n "$A" ] #A是否不为空
[ "file" -ef "file1" ] #file和file1是否为同一档案,可可判断是否为硬链接
[ "file" -ot "file1" ] #file文件是否比file1旧
[ "file" -nt "file1" ] #file文件是否比file1新
示例:
[root@localhost mnt]# test "$A" = "$B" && echo yes || echo no
no
[root@localhost mnt]# test "$A" -lt "$B" && echo yes || echo no
yes
[root@localhost mnt]# test "$A" -gt "$B" && echo yes || echo no
no
[root@localhost mnt]# [ "$A" -ne "$B" -a "$A" -le "$B" ] && echo yes || echo no
yes
[root@localhost mnt]# [ "$A" -eq "$B" -a "$A" -le "$B" ] && echo yes || echo no
no
[root@localhost mnt]# [ "$A" -eq "$B" -o "$A" -le "$B" ] && echo yes || echo no
yes
[root@localhost mnt]# ln file1 file2
[root@localhost mnt]# [ "file1" -ef "file2" ] && echo yes || echo no
yes #file1文件和file2文件是否相同,是输出yes,不是输出no
[root@localhost mnt]# [ "file" -ef "file1" ] && echo yes || echo no #file文件和file1文件是否相同,是输出yes,不是输出no
no
[root@localhost mnt]# [ "file" -ot "file1" ] && echo yes || echo no #file文件是否比file1旧,是输出yes,不是输出no
yes
[root@localhost mnt]# [ "file" -nt "file1" ] && echo yes || echo no #file文件是否比file1新,是输出yes,不是输出no
no
test 命令
[ -e "$1" ] #判断是否存在
[ -f "$1" ] #判断是否为普通文件
[ -L "$1" ] #判断是否为软连接
[ -S "$1" ] #判断是否为套接字
[ -b "$1" ] #判断是否为块设备
[ -d "$1" ] #判断是否为目录
[ -c "$1" ] #判断是否字符设备
示例:
判断某个文件是什么类型的并显示出来
[root@localhost mnt]# vim file_check.sh
#!/bin/bash
[ -z "$1" ] && {
echo Please input a filename
exit 1
}
[ -e "$1" ] || {
echo "$1" is not exist
exit 1
}
[ -L "$1" ] && {
echo "$1" is a symbolic link
exit 0
}
[ -f "$1" ] && {
echo "$1" is a regular file
exit 0
}
[ -S "$1" ] && {
echo "$1" is a socket
exit 0
}
[ -b "$1" ] && {
echo "$1" is a block special
exit 0
}
[ -d "$1" ] && {
echo "$1" is a directory
exit 0
}
[ -c "$1" ] && {
echo "$1" is a character special
}
9.tr 命令
用来对字符进行转换
如:tr ‘A-C’ ‘a-c’ #把A-C转换成a-c
示例:把文件后面的第一个字符大写字母转换为小写字母并且判断是否为hello,如果是输出yes,不是输出no
[root@localhost mnt]# vim file.sh
WORD=$(echo $1 | tr 'A-Z' 'a-z') #把A-Z转换成a-z
[ "$WORD" = "hello" ] &&{
echo yes
}||{
echo no
}
[root@localhost mnt]# sh file.sh hello
yes
[root@localhost mnt]# sh file.sh HELLO
yes
不进行字符转换
/mnt/file.sh内容:
进行字符转换
/mnt/file.sh内容: