shell中常用的基础命令1(diff、patch、cut、sort、uniq、tr、test)

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,53和5列
-f 3-53到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

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值