shell中常用的基础命令

本文详细介绍了Linux中几个常用的命令,包括diff用于比较文件差异,patch用于应用补丁,cut用于截取文件内容,sort用于排序,uniq用于去除重复行,tr用于字符转换。此外,还讲解了条件判断操作符如`&&`和`||`以及test命令的使用,涵盖了文件和数字的比较。这些命令在日常系统管理和脚本编写中非常实用。
摘要由CSDN通过智能技术生成

准备工作:

一台可以和真实主机ping通的虚拟机
真实主机:172.25.254.77
虚拟机:172.25.254.100

建立实验素材:

[root@westosa ~]# cd /mnt/
[root@westosa mnt]# echo westos linux > westos
[root@westosa mnt]# cat westos > westos1
[root@westosa mnt]# echo 123 >> westos1
[root@westosa mnt]# cat westos
westos linux
[root@westosa mnt]# cat westos1
westos linux
123

diff命令

diff  files|directorys

输出信息:

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

常用参数:

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

实验:

root@westosa mnt]# diff westos westos1
1a2
> 123
(2)
[root@westosa mnt]# vim westos1
[root@westosa mnt]# cat westos westos1
westos linux
westos  linux
123
[root@westosa mnt]# diff westos westos1
1c1,2
< westos linux
---
> westos  linux
> 123
(3)
[root@westosa mnt]# diff -b westos westos1  ##忽略空格
1a2
> 123
(4)
[root@westosa mnt]# vim westos
//

[root@westosa mnt]# diff -B westos westos1  ##忽略空行
(5)
[root@westosa mnt]# diff westos westos1
1c1,2
< Westos linux
---
> westos linux
> 
[root@westosa mnt]# diff -i westos westos1
1a2
> 
(6)
[root@westosa mnt]# cat westos
aaaa
Westos linux
bbbb
[root@westosa mnt]# cat westos1
aaaa
westos linux
bbbb
[root@westosa mnt]# vim westos
[root@westosa mnt]# cat westos
aaaa
westos linux
bbbb
[root@westosa mnt]# cat westos1
aaaa
Westos linux
bbbb
[root@westosa mnt]# diff westos westos1
2c2
< Westos linux
---
> westos linux
[root@westosa mnt]# diff -c westos westos1
*** westos	2021-08-13 09:30:28.978153974 +0800
--- westos1	2021-08-13 09:28:26.379153974 +0800
***************
*** 1,3 ****
  aaaa
! Westos linux
  bbbb
--- 1,3 ----
  aaaa
! westos linux
  bbbb

patch用法

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

建立实验素材:

[root@westosa mnt]# mkdir westosdir
[root@westosa mnt]# mkdir westosdir1
[root@westosa mnt]# touch westosdir1/westosfile
[root@westosa mnt]# ls westosdir1/
westosfile

实验:

(1)
[root@westosa mnt]# diff -r westosdir westosdir1/
Only in westosdir1/: westosfile
(2)
[root@westosa mnt]# diff -u  westos westos1 > westos.path
[root@westosa mnt]# cat westos.path
--- westos	2021-08-13 09:30:28.978153974 +0800
+++ westos1	2021-08-13 09:28:26.379153974 +0800
@@ -1,3 +1,3 @@
 aaaa
-Westos linux
+westos linux
 bbbb
(3)
[root@westosa mnt]# dnf install patch  ##下载打补丁用到的工具
(4)
[root@westosa mnt]# patch westos westos.path  ####westos 是原文件 westos.path 补丁文件
patching file westos
[root@westosa mnt]# cat westos
aaaa
westos linux
bbbb
[root@westosa mnt]# cat westos1
aaaa
westos linux
bbbb
[root@westosa mnt]# ls
 westosdir1  westos1  westos westos.path     westosdir  ##当用patch打补丁后原文件内容被修改并且不会备份

(5)
[root@westosa mnt]# patch -b westos westos.path  ##-b 表示备份原文件内容
patching file westos
Reversed (or previously applied) patch detected!  Assume -R? [n] y
[root@westosa mnt]# cat westos
aaaa
Westos linux
bbbb
[root@westosa mnt]# ls
 westos     westosdir1	westos1    westos.path	westosdir	westos.orig  ##westos.orig
[root@westosa mnt]# cat westos.orig
aaaa
westos linux
bbbb

cut用法

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

实验:

[root@westosa mnt]# rm -fr /mnt/*
[root@westosa mnt]# ls
[root@westosa mnt]# cp /etc/passwd .
[root@westosa mnt]# vim passwd
[root@westosa mnt]# cut -d : -f 1 passwd  ##显示文件passwd第一列
root
bin
daemon
[root@westosa mnt]# cut -d : -f 1,7 passwd  ##显示文件passwd第一列和第七列
root:/bin/bash
bin:/sbin/nologin
daemon:/sbin/nologin

[root@westosa mnt]# cut -d : -f 1-3 passwd  ##第一列到第三列
root:x:0
bin:x:1
daemon:x:2
[root@westosa mnt]# cut -d : -f 3- passwd  ##第三列到最后一列
0:0:root:/root:/bin/bash
1:1:bin:/bin:/sbin/nologin
2:2:daemon:/sbin:/sbin/nologin
[root@westosa mnt]# cut -d : -f -3 passwd  ##第一列到第三列
root:x:0
bin:x:1
daemon:x:2
[root@westosa mnt]# cut -c 1-4 passwd  ##所有行的第一个字符到第四个字符
root
bin:
daem

sort用法

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

实验素材:

[root@westosa mnt]# vim westos
[root@westosa mnt]# cat westos 
2
2
66
5
8
9
34
65
87
90
21
2
3
66
21

实验:

[root@westosa mnt]# sort westos   ##排序westos文件,按照每一列的第一个字符大小排序
2
2
2
21
21
3
34
5
65
66
66
8
87
9
90
[root@westosa mnt]# sort -n westos  ##纯数字排序
2
2
2
3
5
8
9
21
21
34
65
66
66
87
90
[root@westosa mnt]# sort -nr westos ##-r 表示倒序
90
87
66
66
65
34
21
21
9
8
5
3
2
2
2
[root@westosa mnt]# sort -nru westos ##u 表示去掉重复部分
90
87
66
65
34
21
9
8
5
3
2
[root@westosa mnt]# sort -nru westos -o test ##输入到指定文件
[root@westosa mnt]# cat test
90
87
66
65
34
21
9
8
5
3
2
[root@westosa mnt]# vim westos 
[root@westosa mnt]# cat westos  ##文件用:分割了两列
2:2
2:2
2:66
a:5
2:8
a:9
2:34
2:65
a:87
a:90
a:21
a:2
3:3
a:66
a:21
[root@westosa mnt]# sort -n -t : -k 2 westos  ##-t 指定分隔符 -k指定排序的列
2:2
2:2
a:2
3:3
a:5
2:8
a:9
a:21
a:21
2:34
2:65
2:66
a:66
a:87
a:90
[root@westosa mnt]# sort -n -t : -k 1 westos   ##排序列为第一列
a:2
a:21
a:21
a:5
a:66
a:87
a:9
a:90
2:2
2:2
2:34
2:65
2:66
2:8
3:3

 uniq用法

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

实验:

[root@westosa mnt]# vim westos 
[root@westosa mnt]# sort -n westos | uniq -c  ##对重复内容进行统计重复个数
      3 2
      1 3
      1 5
      1 8
      1 9
      2 21
      1 34
      1 65
      2 66
      1 87
      1 90
[root@westosa mnt]# sort -n westos | uniq -d  ##显示重复的行
2
21
66
[root@westosa mnt]# sort -n westos | uniq -u  ##显示唯一的行
3
5
8
9
34
65
87
90

tr命令

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

实验:

[root@westosa mnt]# vim westos 
[root@westosa mnt]# cat westos 
westosfile
[root@westosa mnt]# tr  'a-z' 'A-Z' <westos 
WESTOSFILE

 && ||条件判断

&&	符合条件作动作,命令运行成功执行&&之后的指令
||	不符合条件作动作,命令运行失败执行||之后的指令

实验:

[root@westosa mnt]# ping -c1 -w1 172.25.254.100 &> /dev/null
[root@westosa mnt]# ping -c1 -w1 172.25.254.100 &> /dev/null && echo 172.25.254.100 is up || echo 172.25.254.100 is down
172.25.254.100 is up

test命令

test
	test = []	##[] 就相当于test命令,但[后和]前都必须有空格
	"test $a = $b" = [ "$a" = "$b" ]

test数字对比

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

实验:

[root@westosa mnt]# a=1   ##建立a、b两个变量并都赋值为1
[root@westosa mnt]# b=1
[root@westosa mnt]# test "$a" = "$b"
[root@westosa mnt]# test "$a" = "$b" && echo yes || echo no
yes
[root@westosa mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[root@westosa mnt]# test "$a" = "$b"
[root@westosa mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[root@westosa mnt]# [ "$a" != "$b" ] && echo yes || echo no
no
[root@westosa mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
yes
[root@westosa mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
no
[root@westosa mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@westosa mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes
[root@westosa mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
no
test的条件关系
	-a	##并且
	-o	##或者

执行下列脚本来判断数字范围

[root@westosa mnt]# vim num_check.sh
[root@westosa mnt]# sh num_check.sh 10 
10 is not in 0-9
[root@westosa mnt]# sh num_check.sh 9
9 is in 0-9
[root@westosa mnt]# cat num_check.sh
#!bin/bash
[ "$1" -ge "0" -a "$1" -lt "10" ] && {
	echo $1 is in 0-9
}||{
	echo $1 is not in 0-9
}

test对于文件的判定

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

实验:

1)
[root@westosa mnt]# touch /mnt/file1
[root@westosa mnt]# touch /mnt/file2
[root@westosa mnt]# [ "/mnt/file1" -nt "/mnt/file2" ] && echo yes || echo no  ##file1文件和file2文件比哪个更新
no
(2)
[root@westosa mnt]# [ "/mnt/file1" -ot "/mnt/file2" ] && echo yes || echo no  ##file1文件和file2文件比哪个更老
yes
(3)
[root@westosa mnt]#  ls -li
total 8
17208005 -rw-r--r--. 1 root root  0 Aug 13 13:44 file1  ##第一列数据是节点号
17268221 -rw-r--r--. 1 root root  0 Aug 13 13:44 file2
[root@westosa mnt]# [ "/mnt/file1" -ef "/mnt/file2" ] && echo yes || echo no  ##判定两个文件是否有相同的节点号
no
(4)
[root@westosa mnt]# ln -s /mnt/file /mnt/westos
[root@westosa mnt]# ls -l
total 8
-rw-r--r--. 1 root root  0 Aug 13 13:44 file1
-rw-r--r--. 1 root root  0 Aug 13 13:44 file2
-rw-r--r--. 1 root root 99 Aug 13 12:47 num_check.sh
-rw-r--r--. 1 root root 71 Aug 13 11:35 test.sh
lrwxrwxrwx. 1 root root  9 Aug 13 13:51 westos -> /mnt/file
[root@westosa mnt]# [ -L "/mnt/westos" ] && echo yes || echo no  ##判定目标是否是软连接
yes
(5)
[root@westosa mnt]# [ -c "/mnt/westos" ] && echo yes || echo no  ##判定目标是否是字符设备
no
(6)
[root@westosa mnt]# [ -b "/dev/vda" ] && echo yes || echo no  ##判定目标是否是块设备
yes
(7)
[root@westosa mnt]# [ -d "/mnt" ] && echo yes || echo no  ##判定目标是否是目录
yes
(8)
[root@westosa mnt]# [ -f "/mnt/file1" ] && echo yes || echo no  ##判定目标是否是文件
yes
(9)
[root@westosa mnt]# [ -e "/mnt/file2" ] && echo yes || echo no  ##判断文件是否存在
yes
(10)
[root@westosa mnt]# dnf install mariadb-server -y &> /dev/null
[root@westosa mnt]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@westosa mnt]# [ -S "/var/lib/mysql/mysql.sock" ] && echo yes || echo no   ##判定目标是否是套接字
yes

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值