Linux--Shell脚本常用命令
diff
比较两个文件或目录的不同
diff test1 test2
diff test1 test2test2 文件 test 后面有一个空格
-b
不检查空格字符不同diff -b test1 test2
-B
不检查空白行diff test1 test2diff -B test1 test2test2 文件必比 test1 文件多了一行空行
-c
显示全部内容,并标出不同之处diff -c test1 test2test1 与 test2 时间戳不同
test2 多了 一行内容:123
-i
不检查大小写的不同diff test1 test2test1 内容 test
test2 内容第一行:tEst
第二行:123
diff -i test1 test2只显示 test2 不同,多了一行 123
忽略掉检查大小写
-q
仅显示有无差异,不显示详细的信息diff test1 test2diff -q test1 test2提示文件 test1 与 test2 有不同之处,不显示不同之处
-r
比较子目录中的文件ls /d1 /d2/d1/test1/d2/test1diff -r /d1/test1 /d2/test1/d2/test1 比 /d1/test1 多了一行内容:123
/d2/test1 是大写 S
/d1/test1 是小写 s
-u
以合并的方式来显示文件内容的不同diff -u test1 test2
diff -u test1 test2 > test.pathtest1 与 test2 不同之处,导入到 test.path (补丁)文件中
patch
用于不同文件之间打补丁
patch -byum install patch -y
patch -b test1 test.path给 test1 文件打 test.path 补丁 使 test1 文件与 test2 文件相同
cat test1 test2
cut
多用于字符的截取
cut -d指定分隔符
cut -f指定截取的列
cut -c指定截取的字符位置
实验
截取 ifconfig eth0 命令显示中的 ip :“172.25.254.330”
ifconfig eth0
ifconfig eth0 | head -n 2 | tail -n 1 | cut -c 14-28头部2行 | 尾部一行 | 截取第 14 至 28 字符
172.25.254.230
截取 /etc/passwd 文件中第二行的 “nologin”
cat passwd | head -n 2 | tail -n 1 | cut -d : -f 7 | cut -d / -f 3头部 2 行 | 尾部 1 行 | 截取以:分隔的第七列中的以 / 分隔的第三列
head -n 2 | tail -n 1 | cut -d : -f 1,3,5头部 2 行 | 尾部 1行 | 分别截取以 : 分隔的第一、三、五列
sort
用于字符排序
-n
纯数字排序
sort -n test
-r
倒序
sort -r test
sort -rn test
-u
去掉重复数字
sort -u test
sort -nru test
-o
输出到指定文件中-t
指定分隔符-k
指定要排序的列sort -nr test | uniq -c | sort -t " " -k 2指定每行显示一次并且按照分隔符空格排序两列
sort -nr test | uniq -c | sort -t " " -k 2 -r指定每行显示一次并且按照分隔符空格排序两列并且倒序排列
sort -nr test | uniq -c | sort -t " " -k 2 -r | head -1指定每行显示一次并且按照分隔符空格排序两列并且倒序排列抓取第一行
sort -nr test | uniq -c | sort -t " " -k 2 -r -o test1指定每行显示一次并且按照分隔符空格排序两列并且倒序排列输出到 test1 文件内
cat test1
uniq
对重复字符做相应处理
-u
显示唯一的行
uniq -u test
-d
显示重复的行
uniq -d test
-c
每行显示一次并统计重复次数
uniq -c test
&& 和 ||
&&
用来执行条件成立后执行的命令
||
用来执行条件不成立后执行的命令
实验
vim /ping.shping -w1 -c1 $1 > /dev/null && echo "$1 is up" || echo "$1 is down"
sh /ping.sh 172.25.254.30网络可达:172.25.254.30 is up
sh /ping.sh 192.168.0.30网络不可达:192.168.0.30 is down
test
test和[]等同test "$A" == "$B"等同于[ "$A" == "$B" ][ "$A" = "$B" ]A等于B
[ "$A" -eq "$B" ]A等于B
[ "$A" != "$B" ]A不等于B
[ "$A" -ne "$B" ]A不等于B
[ "$A" -ge "$B" ]A大于等于B
[ "$A" -gt "$B" ]A大于B
[ "$A" -le "$B" ]A小于等于B
[ "$A" -lt "$B" ]A小于B
[ -z "$A" ]字符串的长度为空
[ -n "$A" ]字符串的长度非空
test File1 –ef File2两个文件是否为同一个文件,可用于硬连接,主要判断两个文件是否指向同一个inod
test File1 –nt File2判断文件1是否比文件2新
test File1 –ot File2判断文件1比是否文件2旧
Tr
转换大小写
实验
查看 / cpu% 使用率
超过 30% 报警
报警信息导入到日志
df | head -n 2 | tsil -n 1
vim logger.shsh logger.shcat /var/log/messages | tail -n 1
每分钟监视一次 / cpu% 使用率
vim logger.shcat logger.shsh logger.sh执行 job 任务
cat /var/log/messages | tail -n 1
实验
vim /root/check_file.sh[ -z “$1” ] && {
echo “Please input your filename”
exit
}
[ -e “$1” ] || {
echo “$1 is not exist”
exit
}
[ -f “$1” ] && {
echo “$1 is file”
exit
}
[ -d “$1” ] && {
echo “$1 is directory”
exit
}
[ -L"$1" ] && {
echo “$1 is softlinks”
exit
}
[ -S “$1” ] && {
echo “$1 is socket”
exit
}
[ -b “$1” ] && {
echo “$1 is block dev”
exit
}
[-c “$1” ] && {
echo “$1 is character dev”
exit
}cat /root/check_file.sh
测试
- Linux下shell脚本编写:常用命令
- 【shell脚本开发技术】linux中shell script脚本开发编程常用命令与脚本速记指南
- 常用linux命令及shell脚本
- [置顶] linux常用命令与shell脚本编程
- Linux--shell脚本基本设置及常用命令
- Linux新手生存笔记[10]——shell脚本基础3-函数及常用命令
- Linux新手生存笔记[10]——shell脚本基础3-函数及常用命令
- linux常用命令 和shell脚本(必知)
- linux和shell脚本常用命令技巧
- Linux新手生存笔记[10]——shell脚本基础3-函数及常用命令
- Linux下的shell脚本的常用命令
- 手边常用的 linux命令及shell脚本
- Linux~Shell脚本常用命令
- Linux(7) 常用命令扩展 改变用户环境变量 (可以实现登陆就执行shell脚本)
- Linux运维常用shell脚本实例
- Linux常用的系统监控shell脚本
- Linux——note shell常用命令 cut 、sort、unqi、tee、tr、split和shell中连接符&& ||
- 每天一个linux命令(62):sh命令 /Linux中执行shell脚本的4种方法总结
- linux简单命令与Shell脚本
- Linux常用的shell命令汇总