3.1 shell中常用的基础命令

1. diff

[num1,num2] [a|c|d] [num3,num4]

输出信息 说明
num1,num2 第一个文件中的行
num3,num4 第二个文件中的行
a 添加
c 更改
d 删除
< 第一个文件中的内容
> 第二个文件中的内容
常用参数 说明
-b 忽略空格
-B 忽略空行
-i 忽略大小写
-c 显示文件所有内容并标识出不同
-r 对比目录
-u 合并输出
[root@localhost Desktop]# vim file1
[root@localhost Desktop]# vim file2
[root@localhost Desktop]# cat file1
hello!
[root@localhost Desktop]# cat file2
hello!
123
[root@localhost Desktop]# diff file1 file2
1a2
> 123
[root@localhost Desktop]# diff file2 file1
2d1
< 123
[root@localhost Desktop]# cat file1
hello
[root@localhost Desktop]# cat file2
hello!
123
[root@localhost Desktop]# diff file1 file2
1c1,2
< hello
---
> hello!
> 123
[root@localhost Desktop]# diff file2 file1
1,2c1
< hello!
< 123
---
> hello
[root@localhost Desktop]# cat file1 
hello
[root@localhost Desktop]# cat file2
hello!
123
[root@localhost Desktop]# diff -u file1 file2
--- file1	2021-03-08 04:08:44.323434380 -0800
+++ file2	2021-03-08 04:12:40.445655766 -0800
@@ -1 +1,2 @@
-hello
+hello!
+123
[root@localhost Desktop]# diff -u file1 file2 > file.path
[root@localhost Desktop]# cat file.path 
--- file1	2021-03-08 04:08:44.323434380 -0800
+++ file2	2021-03-08 04:12:40.445655766 -0800
@@ -1 +1,2 @@
-hello
+hello!
+123
[root@localhost Desktop]# cat file1
hello!    
[root@localhost Desktop]# cat file2
hello!
[root@localhost Desktop]# diff -b file1 file2
[root@localhost Desktop]# diff file1 file2
1c1
< hello!    
---
> hello!
[root@localhost Desktop]# cat file1
hello!




[root@localhost Desktop]# cat file2
hello!
[root@localhost Desktop]# diff -B file1 file2
[root@localhost Desktop]# diff file1 file2
2,5d1
< 
< 
< 
< 
[root@localhost Desktop]# cat file1
hello!


[root@localhost Desktop]# cat file2
hello!
[root@localhost Desktop]# diff -c file1 file2
*** file1	2021-03-08 04:18:52.877427676 -0800
--- file2	2021-03-08 04:15:43.411052436 -0800
***************
*** 1,3 ****
  hello!
- 
- 
--- 1 ----
[root@localhost Desktop]# cat file1
hello!
123
[root@localhost Desktop]# cat file2
hello!
1234
[root@localhost Desktop]# diff -c file1 file2
*** file1	2021-03-08 04:20:01.989199779 -0800
--- file2	2021-03-08 04:20:13.513161778 -0800
***************
*** 1,2 ****
  hello!
! 123
--- 1,2 ----
  hello!
! 1234
[root@localhost Desktop]# mkdir dir1 dir2
[root@localhost Desktop]# diff -r dir1 dir2
[root@localhost Desktop]# touch dir1/file1
[root@localhost Desktop]# diff -r dir1 dir2
Only in dir1: file1
[root@localhost Desktop]# touch dir2/file2
[root@localhost Desktop]# diff -r dir1 dir2
Only in dir1: file1
Only in dir2: file2

2. patch

patch 原文件 补丁文件
参数-b:备份原文件

[root@localhost Desktop]# cat file1
hello linux!
[root@localhost Desktop]# cat file2
hello linux!
[root@localhost Desktop]# echo 123 >> file2
[root@localhost Desktop]# tail file1 file2
==> file1 <==
hello linux!

==> file2 <==
hello linux!
123
[root@localhost Desktop]# diff -u file1 file2 > file.path
[root@localhost Desktop]# cat file.path 
--- file1	2021-03-08 19:07:22.985557626 -0800
+++ file2	2021-03-08 19:09:10.842201421 -0800
@@ -1 +1,2 @@
 hello linux!
+123
[root@localhost Desktop]# dnf install patch -y

[root@localhost Desktop]# patch file1 file.path
patching file file1
[root@localhost Desktop]# tail file1 file2
==> file1 <==
hello linux!
123

==> file2 <==
hello linux!
123
[root@localhost Desktop]# diff file1 file2
[root@localhost Desktop]# vim file1
[root@localhost Desktop]# cat file1
hello linux!
westos
[root@localhost Desktop]# patch -b file1 file.path
patching file file1
Hunk #1 succeeded at 1 with fuzz 1.
[root@localhost Desktop]# ls
dir1  dir2  file1  file1.orig  file2  file.path
[root@localhost Desktop]# cat file1
hello linux!
123
westos
[root@localhost Desktop]# cat file1.orig 
hello linux!
westos

3. cut

参数 说明
-d 指定符号为分隔符
-f 指定显示的列
-c 指定截取的字符
[root@localhost Desktop]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost Desktop]# cut -d : -f 1 passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@localhost Desktop]# cut -d : -f 1-3 passwd
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6
halt:x:7
mail:x:8
operator:x:11
[root@localhost Desktop]# cut -d : -f 1,3 passwd
root:0
bin:1
daemon:2
adm:3
lp:4
sync:5
shutdown:6
halt:7
mail:8
operator:11
[root@localhost Desktop]# cut -d : -f 3- passwd
0:0:root:/root:/bin/bash
1:1:bin:/bin:/sbin/nologin
2:2:daemon:/sbin:/sbin/nologin
3:4:adm:/var/adm:/sbin/nologin
4:7:lp:/var/spool/lpd:/sbin/nologin
5:0:sync:/sbin:/bin/sync
6:0:shutdown:/sbin:/sbin/shutdown
7:0:halt:/sbin:/sbin/halt
8:12:mail:/var/spool/mail:/sbin/nologin
11:0:operator:/root:/sbin/nologin
[root@localhost Desktop]# cat passwd
root     x 0 0 root /root /bin/bash					//root和x之间有5个空格
bin x 1 1 bin /bin /sbin/nologin
daemon x 2 2 daemon /sbin /sbin/nologin
adm x 3 4 adm /var/adm /sbin/nologin
lp x 4 7 lp /var/spool/lpd /sbin/nologin
sync x 5 0 sync /sbin /bin/sync
shutdown x 6 0 shutdown /sbin /sbin/shutdown
halt x 7 0 halt /sbin /sbin/halt
mail x 8 12 mail /var/spool/mail /sbin/nologin
operator x 11 0 operator /root /sbin/nologin
[root@localhost Desktop]# cut -d " " -f 6 passwd
x
/bin
/sbin
/var/adm
/var/spool/lpd
/sbin
/sbin
/sbin
/var/spool/mail
/root
[root@localhost Desktop]# cut -c 1-4 passwd
root
bin:
daem
adm:
lp:x
sync
shut
halt
mail
oper
[root@localhost Desktop]# cut -c 1,4 passwd
rt
b:
dm
a:
lx
sc
st
ht
ml
or

4. sort

参数 说明
-n 纯数字排序
-r 倒叙
-u 去掉重复
-o 输出到指定文件
-t 指定分隔符
-k 指定排序的列
[root@localhost Desktop]# cat file
9
23
5
6
9
2
8
1
7
8
[root@localhost Desktop]# sort file
1
2
23
5
6
7
8
8
9
9
[root@localhost Desktop]# sort -n file
1
2
5
6
7
8
8
9
9
23
[root@localhost Desktop]# sort -rn file
23
9
9
8
8
7
6
5
2
1
[root@localhost Desktop]# cat file
9
23
5
6
9
2
8
1
7
8
[root@localhost Desktop]# sort -nu file
1
2
5
6
7
8
9
23
[root@localhost Desktop]# cat file
9:4
23:4
5:6
6:8
9:9
2:4
8:5
1:7
7:2
8:1
[root@localhost Desktop]# sort -n file
1:7
2:4
5:6
6:8
7:2
8:1
8:5
9:4
9:9
23:4
[root@localhost Desktop]# sort -t : -k 2 -n file
8:1
7:2
23:4
2:4
9:4
8:5
5:6
1:7
6:8
9:9

5. uniq

参数 说明
-c 合并重复并统计重复个数
-d 显示重复的行
-u 显示唯一的行
[root@localhost Desktop]# cat file
9
23
5
6
9
2
8
1
7
8
[root@localhost Desktop]# sort -n file | uniq -u
1
2
5
6
7
23
[root@localhost Desktop]# sort -n file | uniq -d
8
9
[root@localhost Desktop]# sort -n file | uniq -c
      1 1
      1 2
      1 5
      1 6
      1 7
      2 8
      2 9
      1 23

测试

  1. ifconfig 网卡 可以显示此网卡的信息
    请用命令过滤出此网卡的IP,并输出,只显示IP,其他的信息不显示
[root@localhost Desktop]# ifconfig ens160
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.18  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:10:e1:76  txqueuelen 1000  (Ethernet)
        RX packets 2  bytes 128 (128.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 19  bytes 3297 (3.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost Desktop]# ifconfig ens160 | cut -d " " -f 10 | grep 192
192.168.0.18
[root@localhost Desktop]# ifconfig ens160 | grep "inet" | cut -d " " -f 10
192.168.0.18
  1. 找出能登陆系统用户中UID最大的用户,并显示其名称
[root@localhost Desktop]# sort -n /etc/passwd | grep -E "bash$|sh$" | tail -n 1
yao:x:1001:1001::/home/yao:/bin/bash
[root@localhost Desktop]# sort -n /etc/passwd | grep -E "bash$|sh$" | tail -n 1 | cut -d : -f 1
yao
[root@localhost Desktop]# sort -rnt : -k 3 /etc/passwd | awk -F : '/bash$|sh$/{print $1}' | head -n 1
yao
  1. 编写脚本
    检测用户是否存在
[root@localhost Desktop]# cat westos.sh 
id $1 &>/dev/null && {
echo $1 is exist
}||{
echo $1 is not exist
}
[root@localhost Desktop]# sh westos.sh yao
yao is exist
[root@localhost Desktop]# sh westos.sh lee
lee is not exist

6. tr

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

[root@localhost Desktop]# echo westos WESTOS | tr 'a-z' 'A-Z'
WESTOS WESTOS
[root@localhost Desktop]# echo westos WESTOS | tr 'a-zA-Z' 'A-Za-z'
WESTOS westos
[root@localhost Desktop]# echo westos WESTOS | tr 'w' '1'
1estos WESTOS
[root@localhost Desktop]# echo westos WESTOS | tr 'ws' '1'
1e1to1 WESTOS
[root@localhost Desktop]# echo westos WESTOS | tr 'westos' '1'
111111 WESTOS
[root@localhost Desktop]# echo westos WESTOS | tr 'westos' '12'
122222 WESTOS

7. test

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

  1. 数字对比
符号 说明
= 相等
!= 不等于
-eq 等于
-ne 不等于
-le 小于等于
-lt 小于
-ge 大于等于
-gt 大于
[root@localhost Desktop]# a=1
[root@localhost Desktop]# b=1
[root@localhost Desktop]# [ "$a" = "$b" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ "$a" != "$b" ] && echo yes || echo no
no
[root@localhost Desktop]# [ "$a" -eq "$b" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ "$a" -ne "$b" ] && echo yes || echo no
no
[root@localhost Desktop]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@localhost Desktop]# a=1
[root@localhost Desktop]# b=2
[root@localhost Desktop]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ "$a" -lt "$b" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ "$a" -ge "$b" ] && echo yes || echo no
no
[root@localhost Desktop]# [ "$a" -gt "$b" ] && echo yes || echo no
no
  1. 条件关系
符号 说明
-a 并且
-o 或者
[root@localhost Desktop]# a=1
[root@localhost Desktop]# b=2
[root@localhost Desktop]# [ "$a" -lt "2" -a "$b" -gt "1" ] && echo yes || echo no 
yes
[root@localhost Desktop]# [ "$a" -lt "2" -a "$b" -gt "3" ] && echo yes || echo no 
no
[root@localhost Desktop]# [ "$a" -lt "2" -o "$b" -gt "3" ] && echo yes || echo no 
yes
  1. test对空的判定
符号 说明
-n nozero,判定内容不为空
-z zero,判定内容为空
[root@localhost Desktop]# echo $c

[root@localhost Desktop]# [ -z "$c" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ -n "$c" ] && echo yes || echo no
no
[root@localhost Desktop]# c=1
[root@localhost Desktop]# [ -z "$c" ] && echo yes || echo no
no
[root@localhost Desktop]# [ -n "$c" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ ! -n "$c" ] && echo yes || echo no
no
[root@localhost Desktop]# [ ! -z "$c" ] && echo yes || echo no
yes
  1. test对文件的判定
参数 说明
-ef 文件节点号是否一致(硬链接)
-nt 文件1是否比文件2新
-ot 文件1是否比文件2旧
-d 目录
-S 套接字
-L 软链接
-e 存在
-f 普通文件
-b 块设备
-c 字符设备
[root@localhost Desktop]# touch file
[root@localhost Desktop]# ls
dir1  file  passwd  passwd1  RH124_EXAM  westos.sh
[root@localhost Desktop]# [ "passwd" -nt "file" ] && echo yes || echo no
no
[root@localhost Desktop]# [ "passwd" -ot "file" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ "passwd" -ef "file" ] && echo yes || echo no
no
[root@localhost Desktop]# ls
dir1  file  passwd  passwd1  RH124_EXAM  westos.sh
[root@localhost Desktop]# [ -e "test" ] && echo yes || echo no
no
[root@localhost Desktop]# [ -f "file" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ -f "passwd1" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ -L "passwd1" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ -d "dir1" ] && echo yes || echo no
yes
[root@localhost Desktop]# [ -b "/dev/vda" ] && echo yes || echo no
no
[root@localhost Desktop]# [ -c "/dev/pts/0" ] && echo yes || echo no
yes

测试

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

  2. 编写脚本2

  • sh westos1.sh create lee
    当用户lee存在时,显示lee is exist
    当用户lee不存在时,建立lee,并且显示lee is create
  • sh westos1.sh delete lee
    当lee存在时,删除lee,并显示lee is deleted
    当lee不存在时,什么都不做
  • sh westos1.sh haha lee进行报错:
    Error:wrong option haha ,please input create and delete following scripts
  • sh westos1.sh后面的字符个数不足2个时报错:
    Usage: sh westos1.sh <create|delete><username>
[root@localhost Desktop]# sh westos1.sh hsdhh lee
Error:wrong option hsdhh ,please input create and delete following scripts
[root@localhost Desktop]# sh westos1.sh hsdhh 
Usage: sh westos1.sh <create|delete><username>
[root@localhost Desktop]# sh westos1.sh delete lee
[root@localhost Desktop]# sh westos1.sh create lee
lee is created
[root@localhost Desktop]# sh westos1.sh create lee
lee is exist
[root@localhost Desktop]# sh westos1.sh delete lee
lee is deleted
[root@localhost Desktop]# sh westos1.sh delete lee
[root@localhost Desktop]#

在这里插入图片描述
老师的答案
在这里插入图片描述

8. && ||

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

猜你喜欢

转载自blog.csdn.net/weixin_47133613/article/details/114546559
3.1
今日推荐