shell中常用的基础命令

一、diff

用法:diff [options] files|directorys

输出信息:[num1,num2][a|c|d][num3,num4]

num1,num2        ##第一个文件中的行
a                          ##添加
c                          ##更改
d                          ##删除
<                          ##第一个文件中的内容
>                          ##第二个文件中的内容
num3,num4       ##第二个文件中的行

常用参数:

-b忽略空格
-B忽略空行
-i忽略大小写
-c显示文件所有内容并标示不同
-r对比目录
-u合并输出

 

 二、patch

patch 原文件 补丁文件

-b       ##备份文件

 

 三、cut

-d :指定:为分隔符
-f指定显示的列 5第五列| 3,5 3和5列|3-5 3到5列|5- 第五列以后|-5 到第五列
-c指定截取的字符(数字用法同-f)

 四、sort

-n纯数字排序
-r倒叙
-u去掉重复
-o输出到指定文件
-t指定分隔符
-k指定排序的列

 

 五、uniq

-c合并重复并统计重复个数
-d显示重复的行
-u显示唯一的行

 练习:

1.ifconfig 网卡 可以显示此网卡的信息
显示信息中包含此网卡使用的ip地址
请用命令过滤此ip并在输出时只显示ip其他信息不显示

 2.找出能登陆系统用户中UID最大的用户,并显示其名称

 

六、tr

tr 'a-z' 'A-Z'小写转大写
tr 'A-Z' 'a-z'大写转小写

 七、test

test = [ ]   ##[ ] 就相当于test命令

"test $a = $b" = [ "$a" = "$b" ]

test数字对比

=

=!

-eq等于
-ne不等于
-le小于等于
-lt小于
-ge大于等于
-gt大于

 

[root@westoslinux mnt]# a=1
[root@westoslinux mnt]# b=1
[root@westoslinux mnt]# test "$a" = "$b" && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" != "$b" ] && echo yes || echo no
no
[root@westoslinux mnt]# a=2
[root@westoslinux mnt]# [ "$a" != "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# b=3
[root@westoslinux mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
no

test的条件关系

-a并且
-o或者
[root@westoslinux mnt]# c=5
[root@westoslinux mnt]# a=1
[root@westoslinux mnt]# b=9
[root@westoslinux mnt]# [ "$c" -ge "$a" -a "$c" -le "$b" ] && echo yes
yes || echo no  ##判断c值是不是在a和b之间
[root@westoslinux mnt]# [ "$c" -le "$a" -o "$c" -ge "$b" ] && echo yes || echo no  ##判断c值是不是小于a或大于b
no
[root@westoslinux mnt]# c=20
[root@westoslinux mnt]# [ "$c" -le "$a" -o "$c" -ge "$b" ] && echo yes || echo no
yes

 

test对空的判定

-nnozero 判定内容不为空
-zzero 判定内容为空
[root@westoslinux mnt]# [ -z "$d" ] && echo no || echo yes
no
[root@westoslinux mnt]# [ -n "$d" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ ! -n "$d" ] && echo yes || echo no
yes

 

test对于文件的判定

-ef文件节点号是否一致(硬链)
-nt文件1是不是比文件2新
-ot文件1是不是比文件2老
-d目录
-S套结字
-L软链接
-e存在
-f普通文件
-b快设备
-c字符设备
[root@westoslinux mnt]# touch westos
[root@westoslinux mnt]# touch westos1
[root@westoslinux mnt]# ls -i
17676842 test.sh  17251759 westos  17676846 westos1
[root@westoslinux mnt]# [ "westos" -ef "westos1" ] && echo yes || echo no
no
[root@westoslinux mnt]# rm -fr westos1
[root@westoslinux mnt]# ln /mnt/westos /mnt/westos1
[root@westoslinux mnt]# ls -i
17676842 test.sh  17251759 westos  17251759 westos1
[root@westoslinux mnt]# [ "westos" -ef "westos1" ] && echo yes || echo no
yes

[root@westoslinux mnt]# rm -fr westos1
[root@westoslinux mnt]# touch westos1
[root@westoslinux mnt]# [ "westos" -nt "westos1" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ "westos" -ot "westos1" ] && echo yes || echo no
yes

[root@westoslinux mnt]# [ "westos" -ot "westos1" ] && echo yes || echo no
no
[root@westoslinux mnt]# rm -fr westos1
[root@westoslinux mnt]# touch westos1
[root@westoslinux mnt]# [ "westos" -nt "westos1" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ "westos" -ot "westos1" ] && echo yes || echo no
yes
[root@westoslinux mnt]# 
[root@westoslinux mnt]# [ -f "westos" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -e "westos" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -d "westos" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ -d "/mnt" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -L "westos" ] && echo yes || echo no
no
[root@westoslinux mnt]# ln -s /mnt/westos /mnt/test1
[root@westoslinux mnt]# [ -L "test1" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -b "test1" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ -b "/dev/vda" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -c "/dev/vda" ] && echo yes || echo no
no
[root@westoslinux mnt]# [ -c "/dev/pts/0" ] && echo yes || echo no
yes
[root@westoslinux mnt]# [ -S "/dev/pts/0" ] && echo yes || echo no
no

 

 练习:

file_check.sh 在执行时
如果脚本后未指定检测文件报错“未指定检测文件,请指定”
如果脚本后指定文件不存在报错“此文件不存在”
当文件存在时请检测文件类型并显示到输出中

[root@westoslinux mnt]# vim file_check.sh
[ -z "$*" ] && {
  echo 未指定检测文件,请指定
  exit
  }

[ ! -e "$*" ] && {
    echo "$* is not exist"
    exit
  }
[ -d "$*" ] && {
  echo "$* exists and is a directory"
  exit
  }
[ -L "$*" ] && {
   echo "$* exists and is a symbolic link "
   exit
  }

[ -f "$*" ] && {
  echo "$* exists and is a regular file"
  exit
  }
[ -b "$*" ] && {
  echo "$* exists and is block special"
  exit
  }
[ -c "$*" ] && {
  echo "$* exists and is character special"
  exit
  }
[ -S "$*" ] && {
  echo "$* exists and is a socket"
  exit
  }
                                              

八、&& ||

&&符合条件做动作
||不符合条件做动作

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值