文章目录
1. shell中常用的基础命令
1.1 diff(用于对比不同)
用法: diff [options] files|directorys
1.直接打开vim对比
vimdiff test1 test2
2.diff对比输出信息
输出信息: [num1,num2][a|c|d][num3,num4]
num1,num2 | 第一个文件中的行 |
---|---|
a | 添加 |
c | 更改 |
d | 删除 |
< | 第一个文件中的内容 |
> | 第二个文件中的内容 |
num3,num4 | 第二个文件中的行 |
3.diff对比常用参数
diff中的常用参数 | 用途 |
---|---|
-b | 忽略空格 |
-B | 忽略空行 |
-i | 忽略大小写 |
-c | 显示文件所有内容并标示不同 |
-r | 对比目录 |
-u | 合并输出 |
-b, --ignore-space-change
ignore changes in the amount of white space
-B, --ignore-blank-lines
ignore changes where lines are all blank
-i, --ignore-case
ignore case differences in file contents
-c, -C NUM, --context[=NUM]
output NUM (default 3) lines of copied context
-r, --recursive
recursively compare any subdirectories found
-u, -U NUM, --unified[=NUM]
output NUM (default 3) lines of unified context
1.2 patch(打补丁)
1.patch的安装
dnf install patch -y
2.patch的常用参数
patch 原文件 补丁文件 | 不备份原文件 |
---|---|
patch -b 原文件 补丁文件 | -b备份原文件 |
-b or --backup
Make backup files. That is, when patching a file, rename or copy the original
instead of removing it. When backing up a file that does not exist, an empty,
unreadable backup file is created as a placeholder to represent the nonexistent
file. See the -V or --version-control option for details about how backup file
names are determined.
1.3 cut(截取数据)
1.cut的常用参数
cut常用参数 | 用途 |
---|---|
-d : | 指定:为分隔符 |
-f | 指定显示的列 |
-f 5 | 第五列 |
-f 3,5 | 3和5列 |
-f 3-5 | 3到5列 |
-f 5- | 第五列以后 |
-f -5 到第五列 | |
-c | 指定截取的字符(数字用法同-f) |
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
-f, --fields=LIST
select only these fields; also print any line that contains no delimiter character, unless the -s option is specified
-c, --characters=LIST
select only these characters
2.截取指定列的数据
3.截取指定字符的数据
4.cut命令测试
ifconfig网卡可以显示此网卡的信息,显示信息中包含此网卡使用的ip地址
请用命令过滤此ip并在输出时只显示ip其他信息不显示
方案一:ifconfig ens160 | head -n 2 | tail -n 1|cut -c 14-26
方案二:ifconfig ens160 | head -n 2 | tail -n 1|cut -d " " -f 10
方案三:脚本方式
#!/bin/bash
[ -z "$1" ] && {
echo "Error:no interface name Please input interface following Script!"
exit
}
ifconfig $1 &>/dev/null || {
echo "$1 is not found"
exit
}
ifconfig $1 | head -n 2 | tail -n 1 | cut -d " " -f 10
方案四:
ifconfig ens160 | awk '/inet\>/{print $2}'
1.4 sort(排序)
1.sort的参数
sort参数 | 用途 |
---|---|
-n | 纯数字排序 |
-r | 倒叙 |
-u | 去掉重复 |
-o | 输出到指定文件 |
-t | 指定分隔符 |
-k | 指定排序的列 |
(1)对每行的第一列进行排序
sort testsort
(2)-n 纯数字排序
sort -n testsort
-n, --numeric-sort
compare according to string numerical value
(3)-r 倒序
-r, --reverse
reverse the result of comparisons
sort -r testsort
sort -nr testsort
(3)-u 去掉冗余
-u, --unique
with -c, check for strict ordering; without -c, output only the
first of an equal run
sort -u testsort
sort -nu testsort
(4)-o 输出到指定文件
-o, --output=FILE
write result to FILE instead of standard output
sort -nru testsort -o test.out
(5)-t 指定分隔符(针对多列的情况)
-t, --field-separator=SEP
use SEP instead of non-blank to blank transition
sort -t : testsort
(6)-k 指定排序的列
-k, --key=KEYDEF
sort via a key; KEYDEF gives location and type
sort -t : -k 2 testsort
sort -t : -nk 2 testsort
2.sort的测试
练习:[ ls -l 目录 ]可以显示目录中所有文件的属性,请按照文件大小进行排序并找出最大的2个文件并显示其名称。
方法一:
ls -l |grep total -v |sort -t " " -k 5 -nr |head -n 2 |cut -d " " -f 10
方法二:
ls -S | head -n 2
-S sort by file size, largest first
1.5 uniq(去掉冗余)
1.Uniq的参数
Uniq的参数 | 用途 |
---|---|
-c | 合并重复并统计重复个数 |
-d | 显示重复的行 |
-u | 显示唯一的行 |
(1)-c 合并重复并统计重复个数
-c, --count
prefix lines by the number of occurrences
sort -n testsort |uniq -c
(2)-d 显示重复的行
-d, --repeated
only print duplicate lines, one for each group
sort -n testsort |uniq -d
(3)-u 显示唯一的
-u, --unique
only print unique lines
sort -n testsort |uniq -u
1.6 tr(改变字符大小写)
tr ‘a-z’ ‘A-Z’ | 小写转大写 |
---|---|
tr ‘A-Z’ 'a-z’ | 大写转小写 |
1.7 && ||(是非判断)
&& | 符合条件作动作 |
---|---|
II | 不符合条件作动作 |
1.8 test(用于对比)
1.test的符号
test = [ ] —— [ ] 就相当于test命令
[ "$a" = "$b" ] && echo yes || echo no
2.test数字对比
= | 等于 |
---|---|
!= | 不等于 |
-eq | 等于 |
-ne | 不等于 |
-le | 小于等于 |
-lt | 小于 |
-ge | 大于等于 |
-gt | 大于 |
3.test的条件关系
-a | 并且 |
---|---|
-o | 或者 |
4.test对空的判定
-n | (nozero) 判定内容不为空 |
---|---|
-z | (zero) 判定内容为空 |
5.test对于文件的判定
-ef | 文件节点号是否一致(硬链) |
---|---|
-nt | 文件1是不是比文件2新 |
-ot | 文件1是不是比文件2老 |
-d | 目录 |
-S | 套结字 |
-L | 软连接 |
-e | 存在 |
-f | 普通文件 |
-b | 块设备 |
-c | 字符设备 |
6.test的测试
编写脚本完成以下条件 file_check.sh :在执行时,如果脚本后未指定检测文件,报错“未指定检测文件,请指定” 如果脚本后指定文件不存在报错“此文件不存在” 当文件存在时请检测文件类型并显示到输出
#!/bin/bash
[ -z "$1" ] && {
echo "Error: no check file!"
exit
}
[ -e "$1" ] || {
echo "$1 is not exist!"
exit
}
TYPE=$(ls -l $1 | cut -c 1)
[ "$TYPE" = "l" ] && echo $1 is link file
[ "$TYPE" = "-" ] && echo $1 is common file
[ "$TYPE" = "s" ] && echo $1 is socket
[ "$TYPE" = "c" ] && echo $1 is char
[ "$TYPE" = "b" ] && echo $1 is block