文章目录
前言
$1代表后面跟的第一串字符
shell其实是工具,是一个解释器,行为处理方式
shell脚本是记录我们想做事件的集合,在linux中是按照命令进行开发
一个程序的组成部分:数据(采集到的信息)+结构(对信息处理的逻辑)
熟悉命令是掌握shell的关键
diff (对比不同)
vimdiff用法
编写两个不同文件
[root@rhel7_node1 mnt]# cat westos
rhel7_node1.westos.com
[root@rhel7_node1 mnt]# cat westos.new
hello
rhel7_node1.westos.com
用diff来高亮对比不同
--------------------------------------| hello
rhel7_node1.westos.com | rhel7_node1.westos.com
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
~ | ~
diff用法
diff [options参数] files|directorys
常用参数:
-b(忽略空格)
-B(忽略空行)
-i(忽略大小写)
-c(显示文件所有内容并标示不同)
[root@rhel7_node1 mnt]# diff -c westos westos.new
*** westos 2020-03-28 12:03:00.071869873 +0800
--- westos.new 2020-03-28 12:03:35.898868383 +0800
***************
*** 1 ****
--- 1,2 ----
+ hello
rhel7_node1.westos.com
-r(对比目录)
[root@rhel7_node1 mnt]# mkdir ddd ddd1
[root@rhel7_node1 mnt]# touch ddd/ggg
[root@rhel7_node1 mnt]# diff -r ddd ddd1
Only in ddd: ggg
-u(合并输出)
[root@rhel7_node1 mnt]# diff -u westos westos.new
--- westos 2020-03-28 12:03:00.071869873 +0800
+++ westos.new 2020-03-28 12:03:35.898868383 +0800
@@ -1 +1,2 @@
+hello
rhel7_node1.westos.com
输出信息:
[num1,num2][a|c|d][num3,num4]
num1,num2 (第一个文件中的行)
num3,num4(第二个文件中的行)
a (添加)
c (更改)
d (删除)
< (第一个文件中的内容)
> (第二个文件中的内容)
例:
[root@rhel7_node1 mnt]# diff westos westos.new
0a1
> hello
意味着第一个文件的第0行上添加第二个文件的第一行hello
patch(只更改要变化的部分)
创建补丁文件:
[root@rhel7_node1 mnt]# diff -u westos westos.new > westos.path
[root@rhel7_node1 mnt]# ls
ddd ddd1 westos westos.new westos.path
打补丁:patch 文件 补丁文件
[root@rhel7_node1 mnt]# patch westos westos.path
patching file westos
[root@rhel7_node1 mnt]# cat westos
hello
rhel7_node1.westos.com
对原文件进行备份:patch -b 文件 补丁文件
[root@rhel7_node1 mnt]# patch -b westos westos.path
patching file westos
[root@rhel7_node1 mnt]# ls
westos westos.new westos.orig westos.path
注意:当.orig文件存在的时候不要再去-b备份原文件
cut(截取数据)
cut -d : (指定:为分隔符)
-f (指定显示的列 5第五列|【3,5 3和5列】【3-5 3到5列】【5- 第五列以后【-5 到第五列】)
[root@rhel7_node1 mnt]# 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
...
-c(指定截取的字符,数字用法同-f)
[root@rhel7_node1 mnt]# cut -c 1,4 passwd
rt
b:
dm
...
学员检测:
ifconfig 网卡 可以显示此网卡的信息
显示信息中包含此网卡使用的ip地址
请用命令过滤此ip并在输出时只显示ip,其他信息不显示
作业答案:
[root@rhel7_node1 mnt]# ifconfig ens33 | head -n 2 | tail -n 1 |cut -c 14-25
192.168.0.10
sort(排序)
原文件:
[root@rhel7_node1 mnt]# cat westos
3
2
5
3
6
4
2
555
345
876
564
763
653
42
纯数字排序sort -n
[root@rhel7_node1 mnt]# sort -n westos
2
2
3
3
4
5
6
42
345
555
564
653
763
876
倒叙排序sort -r
[root@rhel7_node1 mnt]# sort -nr westos
876
763
653
564
555
345
42
6
5
4
3
3
2
2
去掉重复sort -u
[root@rhel7_node1 mnt]# sort -nu westos
2
3
4
5
6
42
345
555
564
653
763
876
输出到指定文件sort -o
[root@rhel7_node1 mnt]# sort -nru westos -o westos1
[root@rhel7_node1 mnt]# cat westos1
876
763
653
564
555
345
42
6
5
4
3
2
指定分隔符sort -t
指定排序的列sort -k
按照:为分隔符对第二列排序
[root@rhel7_node1 mnt]# sort -t : -k 2 westos -n
23:2
33:2
4:2
1:3
1:3
44:4
5:6
6:345
2:425
55:555
8:564
53:653
34:763
6:876
学员检测:
ls -l 目录 可以显示目录中所有文件案的属性
请按照文件大小进行排序并找出最大的2个文件并显示其名称
[root@rhel7_node1 mnt]# ls -l / |head -n 23|tail -n 22 |sort -t " " -k 2 -nr| head -n 3
dr-xr-xr-x 290 root root 0 Mar 26 19:07 proc
drwxr-xr-x. 145 root root 8192 Mar 28 15:42 etc
drwxr-xr-x 41 root root 1280 Mar 28 15:43 run
注意:可以用自身的命令及其参数排序
[root@rhel7_node1 ~]# ls -S / | head -n 3
etc
boot
root
uniq(重复检测)
合并重复并统计重复个数uniq -c
[root@rhel7_node1 mnt]# sort -t : -k 2 westos -n | uniq -c
1 23:2
1 33:2
1 4:2
2 1:3
1 44:4
1 5:6
1 6:345
1 2:425
1 55:555
1 8:564
1 53:653
1 34:763
1 6:876
显示重复的行uniq -d
[root@rhel7_node1 mnt]# sort -t : -k 2 westos -n | uniq -d
1:3
显示唯一的行uniq -u
[root@rhel7_node1 mnt]# sort -t : -k 2 westos -n | uniq -u
23:2
33:2
4:2
44:4
5:6
6:345
2:425
55:555
8:564
53:653
34:763
6:876
tr(转换字符的大小写)
原文件
[root@rhel7_node1 mnt]# cat westos
westos
WESTOS
LuJiaXi
小写转大写 tr ‘a-z’ ‘A-Z’
[root@rhel7_node1 mnt]# cat westos |tr 'a-z' 'A-Z'
WESTOS
WESTOS
LUJIAXI
大写转小写 tr ‘A-Z’ ‘a-z’
[root@rhel7_node1 mnt]# cat westos |tr 'A-Z' 'a-z'
westos
westos
lujiaxi
test
test命令
[ ] 就相当于test命令
test = [ ]
"test \$a = \$b" = [ "$a" = "$b" ]
给a,b赋值
[root@rhel7_node1 mnt]# a=1
[root@rhel7_node1 mnt]# b=1
[root@rhel7_node1 mnt]# echo $a
1
[root@rhel7_node1 mnt]# echo $b
1
检测是否相等
[root@rhel7_node1 mnt]# test "$a" = "$b" && echo yes || echo no
yes
(注意:等号两边有空格)
[root@rhel7_node1 mnt]# b=2
[root@rhel7_node1 mnt]# test "$a" = "$b" && echo yes || echo no
no
用test的等价符
[root@rhel7_node1 mnt]# [ "$a" = "$b" ] && echo yes || echo no
no
(注意:中括号两头有空格)
test数字对比
设定数据
[root@rhel7_node1 mnt]# a=1
[root@rhel7_node1 mnt]# b=2
[root@rhel7_node1 mnt]# c=1
=
[root@rhel7_node1 mnt]# [ "$a" = "$c" ] && echo yes || echo no
yes
!=
[root@rhel7_node1 mnt]# [ "$a" != "$c" ] && echo yes || echo no
no
-eq(等于)
[root@rhel7_node1 mnt]# [ "$a" -eq "$c" ] && echo yes || echo no
yes
[root@rhel7_node1 mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
no
-ne(不等于)
[root@rhel7_node1 mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
yes
相当于:
[root@rhel7_node1 mnt]# [ ! "$a" -eq "$b" ] && echo yes || echo no
yes
-le (小于等于)
[root@rhel7_node1 mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@rhel7_node1 mnt]# [ "$a" -le "$c" ] && echo yes || echo no
yes
-lt (小于)
[root@rhel7_node1 mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
yes
[root@rhel7_node1 mnt]# [ "$a" -lt "$c" ] && echo yes || echo no
no
-ge(大于等于)
[root@rhel7_node1 mnt]# [ "$a" -ge "$c" ] && echo yes || echo no
yes
-gt(大于)
[root@rhel7_node1 mnt]# [ "$a" -gt "$c" ] && echo yes || echo no
no
test的条件关系
是否是1-10之间的数字
-a(并且)
[root@rhel7_node1 mnt]# [ "$a" -ge "1" -a "$a" -le "10" ] && echo yes || echo no
-o(或者)
[root@rhel7_node1 mnt]# [ "$a" -lt "1" -o "$a" -gt "10" ] && echo no || echo yes
test对空的判定
判定内容不为空-n (nozero)
判定内容为空-z (zero)
[root@rhel7_node1 mnt]# echo $a
-1
[root@rhel7_node1 mnt]# [ -n "$a" ] && echo yes || echo no
yes
[root@rhel7_node1 mnt]# [ -z "$a" ] && echo yes || echo no
no
test对于文件的判定
-ef ##文件节点号是否一致(硬链)
[root@rhel7_node1 mnt]# touch westos
[root@rhel7_node1 mnt]# ln westos westos1
[root@rhel7_node1 mnt]# ls -li
total 0
13953352 -rw-r--r-- 2 root root 0 Mar 28 15:38 westos
13953352 -rw-r--r-- 2 root root 0 Mar 28 15:38 westos1
[root@rhel7_node1 mnt]# [ "./westos" -ef "./westos1" ] && echo yes || echo no
yes
文件1是不是比文件2新 -nt
[root@rhel7_node1 mnt]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 28 15:36 file1
-rw-r--r-- 1 root root 0 Mar 28 15:36 file2
[root@rhel7_node1 mnt]# [ "./file1" -nt "./file2" ] && echo yes || echo no
no
文件1是不是比文件2老 -ot
[root@rhel7_node1 mnt]# [ "./file1" -ot "./file2" ] && echo yes || echo no
yes
目录 -d
[root@rhel7_node1 mnt]# [ -d "/mnt" ] && echo yes || echo no
yes
套结字 -S
[root@rhel7_node1 mnt]# [ -S "/var/lib/mysql/mysql.sock" ] && echo yes || echo no
yes
软连接 -L
[root@rhel7_node1 mnt]# ln -s /etc/passwd /mnt/
[root@rhel7_node1 mnt]# ls -l
total 0
lrwxrwxrwx 1 root root 11 Mar 28 15:41 passwd -> /etc/passwd
[root@rhel7_node1 mnt]# [ -L "/mnt/passwd" ] && echo yes || echo no
yes
存在 -e
[root@rhel7_node1 mnt]# [ -e "/mnt" ] && echo yes || echo no
yes
普通文件 -f
[root@rhel7_node1 mnt]# [ -f "/mnt/westos" ] && echo yes || echo no
yes
快设备 -b
[root@rhel7_node1 mnt]# [ -b "/dev/nvme0n1" ] && echo yes || echo no
yes
字符设备 -c
[root@rhel7_node1 mnt]# [ -c "/dev/pts/0" ] && echo yes || echo no
yes
学员检测:
编写脚本完成以下条件
file_check.sh 在执行时
如果脚本后未指定检测文件报错“未指定检测文件,请指定”
如果脚本后指定文件不存在报错“此文件不存在”
当文件存在时请检测文件类型并显示到输出中
答案:
#!/bin/bash
[ -z "$1" ] && {
echo "please input it"
exit
}
[ -e "$1" ] || {
echo "$1 is not exist!!"
exit
}
[ -b "$1" ] && {
echo $1 is a block file
exit
}
[ -c "$1" ] && {
echo $1 is a zifu file
exit
}
[ -f "$1" ] && {
echo $1 is a file
exit
}
[ -d "$1" ] && {
echo $1 is a directory
exit
}
[ -S "$1" ] && {
echo $1 is a Socket file
exit
}
测试:
[root@rhel7_node1 mnt]# sh check_file.sh
please input it
[root@rhel7_node1 mnt]# sh check_file.sh aaa
aaa is not exist!!
[root@rhel7_node1 mnt]# sh check_file.sh /dev/pts/0
/dev/pts/0 is a zifu file
[root@rhel7_node1 mnt]# sh check_file.sh /mnt
/mnt is a directory
[root@rhel7_node1 mnt]# sh check_file.sh /mnt/westos
/mnt/westos is a file
[root@rhel7_node1 mnt]# sh check_file.sh /var/lib/mysql/mysql.sock
/var/lib/mysql/mysql.sock is a Socket file
&& ||(是非判断)
符合条件作动作&&
不符合条件作动作||
[root@rhel7_node1 mnt]# ping -c1 -w1 172.25.254.111 &> /dev/null && echo yes || echo no
no
[root@rhel7_node1 mnt]# ping -c1 -w1 192.168.0.10 &> /dev/null && echo yes || echo no
yes
ping同显示yes,ping不通显示no
后记
硬链接与软链接的区别复习
做简单的还可以,做稍微复杂的就有点力不从心