一.什么是shell
shell 也是操作系统中的一个软件。
它包在 linux 内核的外面,为用户和内核之间的交互提供了一个接口。
系统中的命令用 shell 去解释。
shell 接收系统回应的输出并显示其到屏幕中。
bash = GNU Bourne-Again Shell。
二.什么是 shell 脚本
脚本是一种解释型语言
用 shell 脚本保存执行动作
用脚本判定命令的执行条件
用脚本来实现动作的批量执行
三.自动添加信息
[root@localhost ~]# cd /mnt
[root@localhost mnt]# vim /etc/vimrc
[root@localhost mnt]# vim file1.sh##编写新文件时才会自动添加
四.用脚本截取ip
[root@localhost mnt]# vim ip_show.sh
[root@localhost mnt]# chmod +x ip_show.sh
[root@localhost mnt]# sh ip_show.sh ##直接调用脚本
[root@localhost mnt]# /mnt/ip_show.sh ##绝对路径调用脚本
五.脚本中的常用命令
1.diff命令
[root@localhost mnt]# diff westos westos1 ##a表示添加;c表示改变;d表示删除 ##在第一个文件的第二行添加123
[root@localhost mnt]# diff -u westos westos1##以合并的方式显示文件内容的不同
[root@localhost mnt]# diff -u westos westos1 > westos.path##将比较结果保存到westos.path
[root@localhost mnt]# cat westos.path
[root@localhost mnt]# yum install patch -y
[root@localhost mnt]# patch westos westos.path ##给westos打补丁
[root@localhost mnt]# cat westos
##patch命令失败或拒绝接受补丁时,会产生一个和原文件同名,以“rej”为后缀的差异文件
2.cut命令
cut -d ##指定分隔符
cut -f 1,7|1-7 指定截取列
cut -c 1,4|1-4 指定截取的字符位置
[root@localhost mnt]# cp /etc/passwd passwd
[root@localhost mnt]# vim passwd
[root@localhost mnt]# cat passwd
[root@localhost mnt]# cut -d: -f 1 passwd ##以:分隔符截取第一列
[root@localhost mnt]# cut -d : -f 1,7 passwd ##以:分隔符截取第一列和第七列
[root@localhost mnt]# cut -c 1,3 passwd ##截取第一和第三个字符位置
3.&&和||
&& 用来执行条件成立后执行的命令
|| 用来执行条件不成立后执行的命令
[root@localhost mnt]# ping -c1 -w1 172.25.254.66 &> /dev/null && echo 172.25.254.66 is up || echo 172.25.254.66 is down ##若能ping通172.25.254.66为up;反之则为down
编写脚本判断ip是否存在
4.sort命令
sort -n ##纯数字排序
sort -r ##倒序
sort -u ##去掉重复数字
sort -o ##输出到指定文件
sort -t ##指定分隔符
sort -k ##指定要排序的列
[root@localhost mnt]# sort -n linux ##正序排列
[root@localhost mnt]# sort -n linux -o file ##正序排列输出到file
[root@localhost mnt]# sort -r linux -n ##倒序排列
[root@localhost mnt]# sort -t : -k 2 -n linux ##以:为分隔符第二列正序排列
5.uniq命令
uniq -u 显示唯一行
uniq -d 显示重复的行
uniq -c 每行显示一次并重复统计次数
[root@localhost mnt]# sort -n file1 | uniq -c ##将file1文件正序排列,显示有数字重复的次数
[root@localhost mnt]# sort -n file1 | uniq -d ##将file1文件正序排列,显示有重复的数字
[root@localhost mnt]# sort -n file1 | uniq -u ##将file1文件正序排列,显示唯一的数字
对/mnt里所有的文件排序
6.test命令
["$A"="$B"]##等于
["$A"!="$B"]##不等于
["$A"-eq"$B"]##等于
["$A"-ne"$B"]##不等于
["$A"-le"$B"]##小于等于
["$A"-lt"$B"]##小于
["$A"-gt"$B"]##大于
["$A"-ge"$B"]##大于等于=
["$A" -ne "$B" -a "$A" -gt "$B"]和,同时成立
["$A" -ne "$B" -o "$A" -gt "$B"]或者,只有一个成立
[-z "$A"]##空
[-n "$A"]##不为空
编辑脚本判断一个数是不是10以内的数
["file1" -ef "file2"]##节点号是否一致
["file1" -nt "file2"]##是否比这个文件新
["file1" -ot "file2"]##是否比这个文件老
[ -e "file" ]##察看是否存在这个文件
[ -f "file" ]##是否是普通文件
[ -L "file" ]##是否是软连接
[ -S "file" ]##是否是套接字
[ -b "file" ]##是否是块设备
[ -d "file" ]##是否是目录
[ -c "file" ]##是否是字符设备
实验:
编辑脚本检测文件类型
[root@localhost mnt]# vim file_check.sh
[ -z "$1" ]&&{
echo "error:please input a file after script"
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 1
}
[ "-b" "$1" ]&&{
echo "$1 is a block file"
exit 0
}
[ "-S" "$1" ]&&{
echo "$1 is a S file"
exit 0
}
[ "-d" "$1" ]&&{
echo "$1 is a dir"
exit 0
}||{
echo "$1 is a file"
}
7.tr
编辑脚本输入无论大小写时均可通过
编辑脚本创建用户存在时输出结果,不存在时创建该用户