4.1shell中常用的基础命令

1.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    ##合并输出
 

2.patch 

patch  原文件 布丁文件
-b    ##备份原文件
 

实验:
 

cd /mnt
echo westos linux > westos
cat westos > westos1
echo 123 >> westos1
diff westos westos1
diff -b westos westos1
diff -B westos westos1
diff -i westos westos1
diff -c westos westos1
mkdir westosdir
mkdir westosdir1
touch westosdir1/westosfile
ls westosdir1
diff -r westosdir westosdir1/
diff -u westos westos1 > westos.path (生成补丁)
dnf install patch
patch westos westos.path
patch -b westos westos.path

 3.cut 

-d :指定:为分隔符
-f指定显示的列
-c指定截取的字符(数字用法同-f)

  cd /mnt
  rm -rf *
  cp /etc/passwd .
  ls
  cat passwd
  cut -d : -f 1 passwd
  cut -d : -f 1,7 passwd  (第一列和第七列)
  cut -d : -f 1-3 passwd    (第一列到第三列)
  cut -d : -f -3 passwd        (第三列之前)
  cut -d : -f 3- passwd         (第三列之后)
  cut -c 1-4 passwd  (第一个字符到第四个字符)

4.sort

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

     vim westos
     cat westos
1
3
6
9
4
5
3
9
22
97
3
    sort westos  (只排列第一列)
    sort -n westos (纯数字排列)
    sort -nr westos  (纯数字倒叙排列)
    sort -nru westos (纯数字去掉倒叙排列)
    sort -nr westos -o test
    sort -n -t : -k 2 westos

5.uniq

-c合并重复并统计重复个数
-d显示重复的行
-u显示唯一的行
   sort -n westos | uniq -c (纯数字排列,并合并重复并统计重复个数)
   sort -n westos | uniq -d (纯数字排列,并显示重复的行)
   sort -n westos | uniq -u (纯数字排列,并显示独立的行)


学员命令测试
1.ifconfig 网卡 可以显示此网卡的信息
  显示信息中包含此网卡使用的ip地址
  请用命令过滤此ip并在输出时只显示ip其他信息不显示
ifconfig ens3 | head -n 2 | tail -n 1 | cut -d "空格" -f 10
2.找出能登陆系统用户中UID最大的用户,并显示其名称
grep bash /etc/passwd | sort -rn -t : -k 3 | cut -d : -f 1 | head -n1

6.tr

tr 'a-z' 'A-Z' 小写转大写
tr  'A-Z' 'a-z'大写转小写
tr 'A-Z' 'a-z' < test将test中的内容大写转小写

7.test

test = []       [] 就相当于test命令
test "$a" = "$b" = [ "$a" = "$b" ]

test数字对比

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

test的条件关系
-a    并且
-o    或者

 test对空的判定
-n    判定内容不为空
-z     判定内容为空

 实验
vim nmu_check.sh

#!/bin/bash
[ "$1" -ge "0" -a "$1" -lt "10"  ] && {
        echo $1 is  in 0-9
}|| {
        echo $1 is not in 0-9
}
sh nmu_check.sh 5

执行下列脚本来判断用户类型
user_check.sh  用户

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

test对于文件的判定

-ef文件节点号是否一致(硬链)-L软连接
-nt文件1是不是比文件2新-e存在
-ot文件1是不是比文件2老-f普通文件
-d目录-b块设备
-S套接字-c字符设备

(判断是否是目录)  [ -d "westosdir" ] && echo yes || echo no

                  dnf install mariadb-server -y &> /dev/null
(判断是否是套接字)[ -S "/var/lib/mysql/mysql.sock" ] && echo yes || echo no

(判断是否是块设备)[ -b "/dev/vda" ] && echo yes || echo no

                   cd /mnt
                   touch file
                   ln -s /mnt/file /mnt/file1

 (判断是否有软链接)[ -L "/mnt/file1" ] && echo yes || echo no
                                  
  (判断是否是字符设备) [ -c "/dev/pts/0" ] && echo yes || echo no

 实验
1)ping -c1 -w1 172.25.254.100 &> /dev/null && echo 172.25.254.100 is up || echo 172.25.254.100 is down
2)脚本:
vim test.sh
#!/bin/bash 
ping -c1 -w1 172.25.254.100 &> /dev/dull && {
        echo 172.25.254.100 is up 
} || {
        echo 172.25.254.100 is down
}
sh test.sh
 3)检测hello用户在系统中是否存在,存在显示hello is exit,不存在显示hello is not exit
#!/bin/bash
id $1 &> /dev/null && {
        echo $1 is exit
} || {
        echo $1 is not exit
}
sh test.sh hello

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

#!/bin/bash
[ -z "$1" ] && {
        echo "Error:no check file , please input file name!!"
        exit
}

[ ! -e "$1" ] && {
        echo "$1 is not exit!"
        exit
}

[ -L "$1" ] && {
        echo "$1 is link file"
        exit
}
[ -f "$1" ] && {
        echo "$1 is common file"
        exit
}
[ -d "$1" ] && {
        echo "$1 is directory"
        exit
}
[ -b "$1" ] && {
        echo "$1 is block device file"
        exit
}
[ -S "$1" ] && {
        echo "$1 is socket"
        exit
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值