shell常用的基本命令
- diff 用来比较两个文件或者目录的不同
diff westos1 westos2 (westos1第二行多个空格,westos2多第三行空行和第四行)
diff westos2 westos1
-b或--ignore-space-change diff -b westos1 westos2 不检查空格字符的不同
-B或--ignore-blank-lines diff -B westos1 westos2 不检查空白行
-c diff -c westos1 westos2 显示全部内文,并标出不同之处(!)
-i diff -i westos1 westos2 不检查大小写的不同(第一行hello的H有大小写的不同)
-q或--brief diff -q westos1 westos2 仅显示有无差异,不显示详细的信息
-u diff -u westos1 westos2 以合并的方式来显示文件内容的不同
-p --> -c 相当于-c
-r或--recursive diff -r /westos1 /westos2 比较子目录中的文件
显示结果分析:
3 a 4,5 > 第二个文件比第一个文件增加了四到五行
4,5 d 3 < 第一个文件比第二个文件需要删除四到五行
3 c 3,5 改变第一个文件的第三行,第二个文件的三/五行才可以匹配
第一个文件的行数 a|b|c| 第二个文件的行数
-a 表示添加 add
-d 表示删除 delete
-c 表示更改 change
< 表示第一个文件中的内容
> 表示第二个文件中的内容
---是分割线
! 表示行有不同
- path 打补丁
yum install patch -y
patch -b 用于不同文件打布丁
patch -b westos1 westos.path 给westos1文件打westos.path补丁
-
cut 多用于字符截取
-f 指定截取的列
-c 指定截取的字符位置 ,表示和 -表示到
-d 指定分隔符cut -f 1,7|1-7 指定截取第一列和第七列|第一列到第七列 cut -c 1,4|1-4 指定截取第一个和第四个字符|第一个到第四个字符
题:
ifconfig eth0 | head -n 2 | tail -n 1 | cut -d " " -f 10 截取eth0网卡的ip
cut -c 14-28
-
sort 多用于字符排序
-n 纯数字排序 -r 倒序 -u 去掉重复数字 -o 输出到指定文件中 -t 指定分隔符 -k 指定要排序的列
sort -n westos 正序纯数字排序 sort -nr westos 倒序排序 sort -nu westos 去掉重复数字排序
- uniq 对重复字符做相应的处理
-u 显示唯一的行 -d 显示重复的行 -c 每行显示一次并统计重复次数
sort -n westos | uniq -d 排序westos文件显示重复行 sort -n westos | uniq -c 排序westos文件并统计重复次数(第一列为重复出现的次数) sort -n westos | uniq -u 排序westos文件显示唯一的行
sort -nr westos |uniq -c 倒叙排序并且统计重复次数
sort -nr westos |uniq -c |sort -t " " -k 2 倒叙排序并且统计重复次数再以空格符为分隔符排序第二列
sort -nr westos |uniq -c |sort -t " " -k 2 -r 倒叙排序并且统计重复次数再以空格符为分隔符倒序第二列
sort -nr westos |uniq -c |sort -t " " -k 2 -r -o file 倒叙排序并且统计重复次数再以空格符为分隔符倒序第二列输出大批file文件
题:
ls -l |sort -t " " -k 5 -nr | head -n 5 在/etc/*.conf文件中查找最大文件输出其文件名
ls -S | head -n 5
&& 和 ||
&&用来执行条件成立后执行的命令
||用来执行条件不成立后执行的命令ping -c1 -w1 172.25.254.129 &> /dev/null && echo up ||echo down
题:ping主机显示up或者down
vim ping.sh
脚本:
#!/bin/bash
ping -c1 -w1 $1 &> /dev/null &&{
echo $1 is up
}||{
echo $1 is down
}
sh ping.sh ip
- test test命令和[ ]等同
test “ A " = = " A" == " A"=="b” 等同 [“ A " = = " A" == " A"=="B”]
[ "$A" = "$B" ] test "$a" = "$b" && echo yes || echo no 等于
[ "$A" != "$B" ] test "$a" != "$b" && echo yes || echo no 不等于
[ "$A" -eq "$B" ] test "$a" -eq "$b" && echo yes || echo no 等于
[ "$A" -ne "$B" ] test "$a" -ne "$b" && echo yes || echo no 不等于
[ "$A" -le "$B" ] [ "$a" -le "$b" ] && echo yes || echo no 小于等于
[ "$A" -lt "$B" ] [ "$a" -lt "$b" ] && echo yes || echo no 小于
[ "$A" -ge "$B" ] [ "$a" -ge "$b" ] && echo yes || echo no 大于等于
[ "$A" -gt "$B" ] [ "$a" -gt "$b" ] && echo yes || echo no 大于
[ "$A" -ne "$B" -a "$A" -gt "$B" ] -a and 且
[ "$A" -ne "$B" -o "$A" -gt "$B" ] -o or 或
[ -z "$A" ] [ -z "$a" ] && echo yes || echo no 为空zero
[ -n "$A" ] [ -n "$b" ] && echo yes || echo no 不为空
[ "file1" -ef "file2" ] 是否同一节点(ls-i)
[ "file1" -nt "file2" ] 是否建立的晚
[ "file1" -ot "file2" ] 是否建立的早
题:
vim check_num.sh
脚本
#!/bin/bash
[ "$1" -gt "0" -a "$1" -le "10" ]&&{
echo yes
}||{
echo no
}
sh check_num.sh 0
- tr 转换大小写
tr 'a-z' 'A-Z' echo file HELLO | tr 'a-z' 'A-Z' tr 'A-Z' 'a-z' echo file HELLO | tr 'A-Z' 'a-z'
题:每分钟在日志显示/使用率超过30%
df / | tail -n 1 | awk ‘{print $5}’ | cut -d % -f 1
vim check_root.sh
脚本
#!/bin/bash
[ `df / | tail -n 1 | awk '{print $5}' | cut -d % -f 1` -ge "30" ]&&{
logger Warning:System root is full !!
}
at now+1min <<EOF
/mnt/check_root.sh
EOF
[ -e "file" ] 是否存在
[ -f "file" ] 是否是普通文件
[ -L "file" ] 是否是软连接
[ -s "file" ] 是否是套接字
[ -b "file" ] 是否是块设备block
[ -d "file" ] 是否是目录
[ -c "file" ] 是否是字符设备
编辑脚本,检测文件是否存在,是普通文件还是软链接还是目录
vim check_file.sh
脚本
#!/bin/bash
[ -z "$1" ]&&{ 检测$1是否为空
echo "Please input filename following $0"
exit 1
}
[ -e "$1" ]||{ 检测$1是否存在
echo "$1 is not exist!!"
exit 2
}&&{
[ -L "$1" ] &&{ 检测$1是否为软连接
echo $1 is softline!!
exit 0
}
[ -d "$1" ] &&{ 检测$1是否为目录
echo $1 is directory!!
exit 0
}
[ -f "$1" ] &&{ 检测$1是否为普通文件
echo $1 is common file!!
exit 0
}
echo $1 is other file!!
}