shell中常用基本命令

1.diff命令 用来比较两个文件或目录的不同

(1)使用格式

diff [options] target1 target2
diff file1 file2
diff diretcory1 directory2

(2)常用[options]

-b忽略空格字符
-B忽略空白行
-c显示全部内文,并标出不同处
-i忽略大小写
-p与-c相似
-q只显示有无差异,不显示详细内容
-r比较子目录文件
-u补丁,以合并的方式显示文件内容的不同

(3)输出读取

[num1,num2]a|c|d[num3,num4]
输出参数含义
num文件中的行数,左侧为第一个文件中的,右侧为第2个文件中的
aadd 添加
cchange 改变
ddelete 删除
  • 2,4c2,4 表示改变第一个文件中的2,4行才能匹配第二个文件中的2,4行

  • diff -ci file1 file2

  • diff -r /mnt/dir1 /mnt/dir2

  • diff -q file1 file2

  • diff -B file1 file2

2.pacth 补丁 补全文件差异

yum install patch -y
patch -b 
  • diff -u file1 file2 > file.path
    patch -b file1 file.path
    在这里插入图片描述

3.cut 字符截取

(1)使用格式cut [options] file
(2)常用options

-d指定分隔符
-f指定截取的列
-c指定截取的字符位置

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

3.sort 字符排序

(1)使用格式: sort [options] file
(2)常用[options]

-n纯数字排序
-r倒序
-u去掉重复数字
-t指定分隔符
-k指定要排序的列
-o输出到指定文件中
  • sort -nru file
  • sort -t : -k
    在这里插入图片描述

4.uniq 对重复字符做相应的处理

命令含义
uniq -u显示唯一的行
uniq -d显示重复的行
uniq -c每行显示一次并统计重复次数

在这里插入图片描述

5.&&与||

&&执行条件成立后执行的命令
|| 执行条件不成立后执行的命令

6.test命令

test 等同于 [ ]
test " $A"=="$B" 等同于 [“ A " = " A"=" A"="B”]
数值比较:

[ "$A" = "$B" ]     A、B是否相等
[ "$A" != "$B" ]    A、B是否不相等
[ "$A" -eq "$B" ]  (equal) A、B是否相等
[ "$A" -ne "$B" ] (not equal) A、B是否不相等
[ "$A" -ge "$B" ]  (greater and equal )A是否大于等于B
[ "$A" -gt "$B" ]    (greater than)A 是否大于B
[ "$A" -le "$B" ]   (less and equal)A是否小于小于等于B
[ "$A" -lt "$B" ]    (less than)A是否小于B

检查对象是否存在:

[ -z  "$A" ]  参数$A是否为空
[ -n  "$A" ]  参数$A是否不为空

检查文件:

[ -e “file” ]文件是否存在
[ -f “file” ]文件是否为公共文件
[ -L “file”]文件是否为软连接
[ -S “file” ]文件是否为套接字
[ -b “file” ]文件是否为块设备
[ -d “file” ]文件是否为目录
[ -c “file” ]文件是否为字符设备
[ “file1” -ef “file2” ]两文件节点数是否相等
[ “file1” -nf “file2” ]两个文件哪个建立的早
[ “file1” -ot “file2” ]两个文件哪个建立的晚
[root@foundation70 mn]# cd /dev/pts
[root@foundation70 pts]# ls
0  ptmx
[root@foundation70 pts]# [ -c "/dev/pts/0" ]&& echo yes || echo no
yes
[root@foundation70 pts]# ll /dev/pts/0
crw--w----. 1 kiosk tty 136, 0 Aug 20 21:58 /dev/pts/0
[root@foundation70 pts]# ln -s /mn/file /mnt/test
[root@foundation70 mn]# ls
file  file1  test  test.sh
[root@foundation70 mn]# ll
total 12
-rw-r--r--. 1 root root  20 Aug 20 21:02 file
-rw-r--r--. 1 root root  15 Aug 20 21:02 file1
lrwxrwxrwx. 1 root root   8 Aug 20 22:00 test -> /mn/file
-rwxr-xr-x. 1 root root 159 Aug 20 21:51 test.sh
[root@foundation70 mn]# [ -L "/mn/test" ]&& echo yes || echo no
yes
[root@foundation70 mn]# [ -e "/mn/test" ]&& echo yes || echo no
yes
[root@foundation70 mn]# [ -f "/mn/test" ]&& echo yes || echo no
yes
[root@foundation70 mn]# [ -b "/mn/test" ]&& echo yes || echo no
no
[root@foundation70 mn]# [ -b "/dev/sda1" ]&& echo yes || echo no
yes
[root@foundation70 mn]# [ -d "/dev/sda1" ]&& echo yes || echo no
no

多条件的连接:

[ "$A" -gt "0" -a "$A" -le "10" ]   and 且  ##$A大于0且小于等于10,两个条件同时满足
[ "$A" -gt "10" -o "$A" -le "0" ]   or  或  ##$A大于10或小于0 ,二者满足一项就好

7.tr 大小写转换

  • echo hello HELLO | tr ‘a-z’ ‘A-Z’
  • echo hello HELLO | tr ‘A-Z’ ‘a-z’

8. 牛刀小试

(1)编写简单脚本:当/的挂载比重超过10%,日志中就会生成警告

    [root@foundation70 mn]# df / | tail -n 1 | cut -d " " -f 6
    12%
    [root@foundation70 mn]# df / | tail -n 1 | cut -d " " -f 6 |cut -d % -f 1
    12
    [root@foundation70 mn]# vim test.sh
      1 #!/bin/bash
  2 [ `df / | tail -n 1 | cut -d " " -f 6 |cut -d % -f 1` -ge "10" ] && {
  3                 logger Warning: System root is full!!
  4 }
  5 at now+1min <<EOF
  6 /mn/test.sh
  7 EOF
~              
    [root@foundation70 mn]# vim test.sh
    [root@foundation70 mn]# chmod +x test.sh 
    [root@foundation70 mn]# ./test.sh 
    job 12 at Tue Aug 20 21:53:00 2019
    [root@foundation70 mn]# at -l
    12	Tue Aug 20 21:53:00 2019 a root
    [root@foundation70 mn]# > /var/log/messages
    [root@foundation70 mn]# date
    Tue Aug 20 21:53:53 CST 2019
    [root@foundation70 mn]# cat /var/log/messages
    Aug 20 21:53:00 foundation70 systemd: Created slice user-0.slice.
    Aug 20 21:53:00 foundation70 systemd: Starting user-0.slice.
    Aug 20 21:53:00 foundation70 systemd-logind: New session 93 of user root.
    Aug 20 21:53:00 foundation70 systemd: Started Session 93 of user root.
    Aug 20 21:53:00 foundation70 systemd: Starting Session 93 of user root.
    Aug 20 21:53:00 foundation70 root: Warning: System root is full!!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值