shell从入门到精通

系列文章目录

shell从入门到精通

shell从入门到精通


# shell脚本常用基础命令1

shell脚本常用基础命令


一、diff 用法 (一般作补丁,用补丁的方式更新脚本)

Usage: diff [OPTION] files|dir

输出信息:
【num1,num2】a|c|d 【num3,num4】
之前是第一个文件的行
之后是第二个文件的行
a 添加
c 更改
d 删除
<第一个文件的内容
">"第二个文件的内容

1.1参数a添加

[root@docker3 mnt]# vim yan
[root@docker3 mnt]# cat yan > yan1
[root@docker3 mnt]# ls
yan  yan1
[root@docker3 mnt]# vim yan1
[root@docker3 mnt]# cat yan
hello yan
[root@docker3 mnt]# cat yan1
hello yan
123
[root@docker3 mnt]# diff yan yan1
1a2
> 123

1.2 参数c更改

[root@docker3 mnt]# vim yan
[root@docker3 mnt]# cat yan
hello yan
456
[root@docker3 mnt]# cat yan1
hello yan
123
[root@docker3 mnt]# diff yan yan1
2c2
< 456
---
> 123
[root@trade shell]# diff yan yan1
1,2c1,2
< hello yan
< 123
---
> Ahello yan
> 456
[root@docker3 mnt]# cat yan
Ahello yan
456
123
[root@docker3 mnt]# cat yan1
hello yan
123
000
[root@docker3 mnt]# diff yan yan1
1,2c1
< Ahello yan
< 456
---
> hello yan
3a3
> 000

1.3参数d删除

[root@docker3 mnt]# cat yan
hello yan
[root@docker3 mnt]# cat yan1
hello yan
123
[root@docker3 mnt]# diff yan1 yan
2d1
< 123

1.4参数a和d的对比

[root@trade shell]# cat yan
yan
[root@trade shell]# cat yan1
yan
123
[root@trade shell]# diff yan yan1
1a2
> 123
[root@trade shell]# diff  yan1 yan
2d1
< 123

常用参数

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

1.5参数b(忽略空格)

[root@docker3 mnt]# cat yan
hello yan
[root@docker3 mnt]# cat yan1
hello   yan
[root@docker3 mnt]# diff yan yan1
1c1
< hello yan
---
> hello   yan
[root@docker3 mnt]# diff -b yan yan1

1.6 参数B(忽略空行)

[root@docker3 mnt]# cat yan
hello yan
空行
[root@docker3 mnt]# cat yan1
hello yan
[root@docker3 mnt]# diff yan yan1
2d1
< 
[root@docker3 mnt]# diff -B yan yan1

1.7 参数i(忽略大小写)

[root@docker3 mnt]# cat yan 
Hello yan
[root@docker3 mnt]# cat yan1
hello yan
[root@docker3 mnt]# diff yan yan1
1c1
< Hello yan
---
> hello yan
[root@docker3 mnt]# diff -i yan yan1

1.8 参数c(显示文件所有内容并标识不同)

[root@docker3 mnt]# cat yan
123
456
Hello yan
123
1234
[root@docker3 mnt]# cat yan1
123
456
Hello yan
o23
1234
[root@docker3 mnt]# diff yan yan1
4c4
< 123
---
> o23
[root@docker3 mnt]# diff -c yan yan1
*** yan	2022-02-17 06:33:56.176000000 +0800
--- yan1	2022-02-17 06:34:12.207000000 +0800
***************
*** 1,5 ****
  123
  456
  Hello yan
! 123
  1234
--- 1,5 ----
  123
  456
  Hello yan
! o23
  1234

1.9参数r(对比目录)

[root@docker3 mnt]# mkdir yan2 yan3
[root@docker3 mnt]# touch yan2/yan
[root@docker3 mnt]# diff -r yan2 yan3
Only in yan2: yan

1.10 -u参数 合并输出 (生成补丁)

[root@docker3 mnt]# cat yan
123
456
Hello yan
123
1234
[root@docker3 mnt]# cat yan1
123
456
Hello yan
o23
[root@docker3 mnt]# diff -u yan yan1
--- yan	2022-02-17 06:33:56.176000000 +0800
+++ yan1	2022-02-17 06:34:12.207000000 +0800
@@ -1,5 +1,5 @@
 123
 456
 Hello yan
-123
+o23
 1234
[root@docker3 mnt]# diff -u yan yan1 > yan.path

二、path命令

path用法 打补丁
patch 原文件 补丁文件
-b 备份原文件

[root@docker3 mnt]# yum install patch -y
[root@docker3 mnt]# patch -b yan yan.path 
[root@docker3 mnt]# cat yan
123
456
Hello yan
o23
1234
[root@docker3 mnt]# cat yan.path 
--- yan	2022-02-17 06:33:56.176000000 +0800
+++ yan1	2022-02-17 06:34:12.207000000 +0800
@@ -1,5 +1,5 @@
 123
 456
 Hello yan
-123
+o23
 1234
[root@docker3 mnt]# cat yan.orig -b备份的原文件
123
456
Hello yan
123
1234

三、cut用法 截取数据

cut
-d : 指定:为分割符
-f 指定显示的列 5第五列 | 3,5第三列和第五列 | 3-5第三列到第五列 | 5- 第五列以后 | -5 到第五列
-c 指定截取字符,(用数字用法和-f一样)
取出第五行

[root@docker3 mnt]# cp /etc/passwd .
[root@docker3 mnt]# head -n 5 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
[root@docker3 mnt]# head -n 5 passwd | tail -n1
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

取出第五列

[root@docker3 mnt]# cut -d : -f 5 passwd 
root
bin
daemon
adm
lp
sync
shutdown
halt
games

第一列和第五列

[root@docker3 mnt]# cut -d : -f 1,5 passwd 
root:root
bin:bin
daemon:daemon
adm:adm
lp:lp
sync:sync
shutdown:shutdown
halt:halt
games:games
[root@docker3 mnt]# cut -c1-4 passwd 
root
bin:
daem
adm:
lp:x
sync
shut
halt
game
tss:

四、sort 排序

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

[root@docker3 mnt]# sort yan
1
123
13
2
3
4
7
81
81
[root@docker3 mnt]# sort -n yan
1
2
3
4
7
13
81
81
123
[root@docker3 mnt]# sort -rn yan
123
81
81
13
7
4
3
2
1
[root@docker3 mnt]# sort -rn yan -o hello
[root@docker3 mnt]# cat hello 
123
81
81
13
7
4
3
2
1
[root@docker3 mnt]# sort -n yan
a:1
a:123
a:13
a:2
a:3
a:4
a:7
a:81
a:81
[root@docker3 mnt]# sort -t : -k 2 -n yan
a:1
a:2
a:3
a:4
a:7
a:13
a:81
a:81
a:123
[root@docker3 mnt]# sort -un yan
1
2
3
4
7
13
81
123

END

shell脚本常用基础命令1


shell脚本常用基础命令


一、 uniq指令

1.1参数c(对于顺序的合并重复并且统计个数)

[root@trade shell]# cat yan
123
4
1
2
3
7
81
13
81
13
81
81
123
4
123
[root@trade shell]# uniq -c yan 
      1 123
      1 4
      1 1
      1 2
      1 3
      1 7
      1 81
      1 13
      1 81
      1 13
      2 81
      1 123
      1 4
      1 123

1.2uniq和sort配合使用(合并重复并统计重复个数)

[root@docker3 mnt]# sort -n yan | uniq -c 
      1 2
      1 3
      1 4
      1 7
      1 13
      2 81
      1 123

1.3参数d(显示重复的行)

[root@docker3 mnt]# sort -n yan | uniq -d
81

1.4参数u(显示唯一的行)

[root@docker3 mnt]# sort -n yan | uniq -u
1
2
3
4
7
13
123

1.5抓取出现数字最多的数字(不含重复出现最多数字一样)

[root@trade shell]# cat yan
123
4
1
2
3
7
81
13
81
13
81
81
123
4
123
[root@trade shell]# sort -n yan | uniq -c
      1 1
      1 2
      1 3
      2 4
      1 7
      2 13
      4 81
      3 123
[root@trade shell]# sort -n yan | uniq -c | sort -n -k 1
      1 1
      1 2
      1 3
      1 7
      2 13
      2 4
      3 123
      4 81
[root@trade shell]# sort -n yan | uniq -c | sort -n -k 1 | tail -n1
      4 81
[root@trade shell]# sort -n yan | uniq -c | sort -n -k 1 | tail -n 1 | cut -d " " -f 8
81

1.6抓取网卡的ip

[kiosk@foundation38 Desktop]$ ifconfig wlan0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.31.177  netmask 255.255.255.0  broadcast 192.168.31.255
        inet6 fe80::cd76:22a3:151a:9e8d  prefixlen 64  scopeid 0x20<link>
        ether b0:68:e6:99:ba:7d  txqueuelen 1000  (Ethernet)
        RX packets 34279  bytes 33490309 (31.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21281  bytes 5558502 (5.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[kiosk@foundation38 Desktop]$ ifconfig wlan0 | head -n 2
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.31.177  netmask 255.255.255.0  broadcast 192.168.31.255
[kiosk@foundation38 Desktop]$ ifconfig wlan0 | head -n 2 | tail -n 1
        inet 192.168.31.177  netmask 255.255.255.0  broadcast 192.168.31.255
[kiosk@foundation38 Desktop]$ ifconfig wlan0 | head -n 2 | tail -n1 | cut -d " " -f 10
192.168.31.177

二、tr用法

tr用法
tr ‘a-z’ ‘A-Z’ 小写转换成大写
tr ‘A-Z’ ‘a-z’ 大写转换成小写

[root@docker3 mnt]# cat yan
hello
HELLO
[root@docker3 mnt]# tr 'l' 's' < yan
hesso
HELLO
[root@docker3 mnt]# tr 'a-z' 'A-Z' < yan
HELLO
HELLO
[root@docker3 mnt]# cat yan
hello
HELLO

三、shell脚本条件

&&条件成立则…(是)
||不成立则…(否)

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

四、test指令

4.1test用法

test是作比较的

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

test数字对比

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

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

4.2test的条件关系

-a 并且
-o 或者

[root@docker3 mnt]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no
yes
[root@docker3 mnt]# a=20
[root@docker3 mnt]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" -gt "0" -o "$a" -lt "10" ] && echo yes || echo no
yes

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

[root@docker3 mnt]# [ -z "$c" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ -n "$c" ] && echo yes || echo no
no

五、脚本应用

5.1执行脚本来判断用户的类型

user_check.sh 用户
用户类型为
super user 0
system user 1-999
common user

$*表示脚本后面跟的所有字符串
在这里插入图片描述

[root@docker3 mnt]# sh user_check.sh 
error : Please input username
[root@docker3 mnt]# sh user_check.sh yan
yan is common user
[root@docker3 mnt]# sh user_check.sh root
root is super user
[root@docker3 mnt]# sh user_check.sh out
ERROR: out is not exist

5.2执行脚本判断文件类型

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

[root@docker3 mnt]# [ -e "/mnt" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ -d "/mnt" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ -S "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ -L "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ -f "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ -c "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "/mnt/yan" -ef "/mnt/yan1" ] && echo yes || echo no 
no
[root@docker3 mnt]# [ "/mnt/yan" -ef "/mnt/yan2" ] && echo yes || echo no 
yes
[root@docker3 mnt]# [ "/mnt/yan" -ot "/mnt/file" ] && echo yes || echo no 
no
[root@docker3 mnt]# [ "/mnt/yan" -nt "/mnt/file" ] && echo yes || echo no 
yes

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

END

shell脚本常用基础命令2


shell脚本常用基础命令


文本处理三剑客:grep sed awk

一、grep用法

grep -E = egrep (扩展搜索正文表达式)
grep格式
grep 匹配条件 处理文件(很多正文表达式用不了) |表示或者

[root@docker3 mnt]# grep "bash|root" passwd 
[root@docker3 mnt]# egrep "bash|root" passwd 
root:x:0:0:root:/root:/bin/bash
test:root:test
operator:x:11:0:operator:/root:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash
[root@docker3 mnt]# grep -E "bash|root" passwd 
root:x:0:0:root:/root:/bin/bash
test:root:test
operator:x:11:0:operator:/root:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash
[root@docker3 mnt]# grep -e bash -e root passwd 
root:x:0:0:root:/root:/bin/bash
test:root:test
operator:x:11:0:operator:/root:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash

grep开启的是贪婪模式,要是不匹配前面的<,不匹配后面>

[root@docker3 mnt]# grep -E "root" passwd 
root:x:0:0:root:/root:/bin/bash
test:root:test
chroot
rootch
[root@docker3 mnt]# grep -E "\<root" passwd 
root:x:0:0:root:/root:/bin/bash
test:root:test
rootch
operator:x:11:0:operator:/root:/sbin/nologin
[root@docker3 mnt]# grep -E "\<root\>" passwd 
root:x:0:0:root:/root:/bin/bash
test:root:test
operator:x:11:0:operator:/root:/sbin/nologin

grep 前面加数字,表示该关键字前面后面几行都显示出来

[root@docker3 mnt]# grep chroot passwd 
chroot
[root@docker3 mnt]# grep -2 chroot passwd 
test:root:test
TEST:ROOT:TEST
chroot
rootch
operator:x:11:0:operator:/root:/sbin/nologin
[root@docker3 mnt]# grep -A2 chroot passwd 
chroot
rootch
operator:x:11:0:operator:/root:/sbin/nologin
[root@docker3 mnt]# grep -B2 chroot passwd 
test:root:test
TEST:ROOT:TEST
chroot

显示匹配的在第几行

[root@docker3 mnt]# grep -n chroot passwd 
12:chroot

反向过滤,除了chroot其它行都过滤出来,展示部分。

[root@docker3 mnt]# grep -v chroot passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash

以^…开头,…¥以…结尾

[root@docker3 mnt]# grep -E "^root" passwd 
root:x:0:0:root:/root:/bin/bash
rootch
[root@docker3 mnt]# grep -E "bash$" passwd 
root:x:0:0:root:/root:/bin/bash
yan:x:1000:1000::/home/yan:/bin/bash
[root@docker3 mnt]# grep -E "ws" yan
ws
[root@docker3 mnt]# grep -E "w.s" yan
was
[root@docker3 mnt]# grep -E "w...s" yan
waaas
[root@docker3 mnt]# grep -E "w.*s" yan
ws
was
waaas
waaaas
waaaaas
wabababs
[root@docker3 mnt]# grep -E "w.?s" yan
ws
was
[root@docker3 mnt]# grep -E "w.+s" yan
was
waaas
waaaas
waaaaas
wabababs
[root@docker3 mnt]# grep -E "w.{3}s" yan
waaas
[root@docker3 mnt]# grep -E "w.{2,3}s" yan
waaas
[root@docker3 mnt]# grep -E "w.{,3}s" yan
ws
was
waaas
[root@docker3 mnt]# grep -E "w.{3,}s" yan
waaas
waaaas
waaaaas
wabababs
[root@docker3 mnt]# grep -E "wab{1,}s" yan
[root@docker3 mnt]# grep -E "w(ab){1,}s" yan
wabababs

脚本
请显示系统中能被su命令切换的用户名称
在这里插入图片描述

[root@docker3 mnt]# sh user_check.sh 
root
yan

二、sed用法

用来对文本的内容进行处理,修改的是模式空间(内存),真实的(磁盘中)
sed 参数 命令 处理对象
sed 参数 处理对象 -f 处理规则文件

对字符的处理
p 显示
d 删除
a 添加
n 取消默认输出,sed默认会输出所有文本内容,使用-n参数后只显示处理过的行
i 插入,直接修改文件内容
c 替换
w 写入
r 整合文件

2.1p参数 (显示)n参数(只显示处理过的行)

[root@docker3 mnt]# sed -n 5p passwd 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@docker3 mnt]# vim rule
[root@docker3 mnt]# cat rule 
5p
[root@docker3 mnt]# sed -n -f rule passwd 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@docker3 mnt]# sed  '5p' fstab 
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011
[root@docker3 mnt]# sed  -n '5p' fstab 
#5
[root@docker3 mnt]# sed  -n '3,5p' fstab 
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
[root@docker3 mnt]# sed  -n '3p;5p' fstab 
# /etc/fstab3
#5

#开头的行以及不是#开头的行

[root@docker3 mnt]# sed -n '/^#/p' fstab 
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
[root@docker3 mnt]# sed -n '/^#/!p' fstab 
1
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011

^$空行

[root@docker3 mnt]# sed -n '/^#/!p' fstab | sed -n '/^$/!p'
1
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011

最后一行$p

[root@docker3 mnt]# sed -n '$p' fstab
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011
[root@docker3 mnt]# sed -n '5,$p' fstab
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011

显示行号,‘=’, '$='最后一行的行号(可以统计文件有多少行)

[root@docker3 mnt]# sed -n '$=' fstab 
11
[root@docker3 mnt]# sed -n -e '3p' -e '5p' fstab 
# /etc/fstab3
#5

d参数删除

[root@docker3 mnt]# sed 5d fstab 
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011
[root@docker3 mnt]# sed '3,5d' fstab 
1
#2
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011
[root@docker3 mnt]# sed '3d;5d' fstab 
1
#2
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011
[root@docker3 mnt]# sed '/^#/d' fstab 
1
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011

a参数添加

[root@docker3 mnt]# sed '/^#/ahello' fstab 
1
#2
hello
# /etc/fstab3
hello
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
hello
#5
hello
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
hello
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
hello
#8
hello
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011

最后一行后面添加

[root@docker3 mnt]# sed '$ahello' fstab 
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011
hello
[root@docker3 mnt]# sed '$ahello\nyan' fstab 
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011
hello
yan

i参数插入 i是在最后一行前面,a是添加到最后一行

[root@docker3 mnt]# sed '$ihello\nyan' fstab 
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
hello
yan
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011
[root@docker3 mnt]# sed '1ihello\nyan' fstab 
hello
yan
1
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011

c参数替换

[root@docker3 mnt]# sed '/^#/chello' fstab 
1
hello
hello
hello
hello
hello
hello
hello
/dev/mapper/rhel-root   /                       xfs     defaults        0 09
UUID=cf9ffcaf-d595-4545-9fd9-3573d573976e /boot  xfs  defaults        0 010
/dev/mapper/rhel-swap   swap                    swap    defaults        0 011

w参数写入

[root@docker3 mnt]# sed '/^#/w file ' fstab 
[root@docker3 mnt]# cat file\  
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8
就是
[root@docker3 mnt]# sed -n '/^#/p' fstab > file
[root@docker3 mnt]# cat file
#2
# /etc/fstab3
# Created by anaconda on Sun Mar 28 10:44:35 2021 4
#5
# Accessible filesystems, by reference, are maintained under '/dev/disk'6
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info7
#8

r

[root@docker3 mnt]# cat yan
1
2
3
[root@docker3 mnt]# cat yan1
4
5
6
[root@docker3 mnt]# cat yan yan1
1
2
3
4
5
6
[root@docker3 mnt]# cat yan yan1 > file

倒叙

[root@docker3 mnt]# tac yan
3
2
1
[root@docker3 mnt]# tac yan yan1
3
2
1
6
5
4
[root@docker3 mnt]# tac yan1 yan
6
5
4
3
2
1

cat tac无法把文件yan1整合到数字1后面,可以正序也可以倒序
1r第一行后面

[root@docker3 mnt]# sed '1ryan1' yan
1
4
5
6
2
3

整合到某关键字后面

[root@docker3 mnt]# sed '/2/ryan1' yan
1
2
4
5
6
3

更改到原文件

[root@docker3 mnt]# sed '/2/ryan1' -i yan
[root@docker3 mnt]# cat yan
1
2
4
5
6
3

脚本
Apache_port.sh
此脚本接入数字
http的端口就改为此数字
假设selinux为关闭状态
在这里插入图片描述

[root@docker3 mnt]# sh Apacha_port.sh 
Error: Please input port number following script!!!
[root@docker3 mnt]# sh Apacha_port.sh 80
Error:Apache is not installed!!

双引号不能被引用变量,单引号会注释变量,所以Listen注意是要双引号
在这里插入图片描述

[root@docker3 mnt]# sh Apacha_port.sh 8080
[root@docker3 mnt]# netstat -antlp | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      4140/httpd       
[root@docker3 mnt]# sh Apacha_port.sh 80
[root@docker3 mnt]# netstat -antlp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      4172/httpd          

sed字符替换
s是全文的行,g全文的列
部分展示 /表示分割符

[root@docker3 mnt]# sed 's/sbin/hello/g' passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/hello/nologin
daemon:x:2:2:daemon:/hello:/hello/nologin
adm:x:3:4:adm:/var/adm:/hello/nologin
lp:x:4:7:lp:/var/spool/lpd:/hello/nologin
sync:x:5:0:sync:/hello:/bin/sync
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/hello/nologin
postfix:x:89:89::/var/spool/postfix:/hello/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/hello/nologin
yan:x:1000:1000::/home/yan:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/hello/nologin
[root@docker3 mnt]# sed 's/:/  /g' 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
games  x  12  100  games  /usr/games  /sbin/nologin
[root@docker3 mnt]# sed 's/:/  /' 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
[root@docker3 mnt]# sed '1,5s/:/  /' 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
[root@docker3 mnt]# sed '/lp/,/halt/s/:/  /g' 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@docker3 mnt]# sed '$s/:/  /g' passwd 
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash
apache  x  48  48  Apache  /usr/share/httpd  /sbin/nologin
[root@docker3 mnt]# sed '$s/:/  /g;s/sbin/hello/g'  passwd 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/hello/nologin
daemon:x:2:2:daemon:/hello:/hello/nologin
adm:x:3:4:adm:/var/adm:/hello/nologin
lp:x:4:7:lp:/var/spool/lpd:/hello/nologin
sync:x:5:0:sync:/hello:/bin/sync
shutdown:x:6:0:shutdown:/hello:/hello/shutdown

替换/,/需要转义,因为它是分割符

[root@docker3 mnt]# sed 's/:/  /g;s/\//##/g'  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

/表示分割符,可以换成@

[root@docker3 mnt]# sed 's@:@ @g;s@/@##@g' 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

END

系列文章目录

shell三剑客之awk 2

shell三剑客之awk


一、awk用法

awk 用法
awk -F 分割符(没加默认是空格,不管多少个空格都算一个)BEGIN{ } 可加条件 { } END { } FILENAME(文件名称本身)

NR 行数NF 列数
¥0 所有的列¥1第一列
¥2第两列¥3第三列

“ ”双引号,表示字符串本身
没有加双引号表示字符串的变量
部分:先打印yan,在打印第五列,最后打印end
条件
/bash$/ 条件
/条件1|条件2/ 条件1或者条件2
/条件1/||/条件2/ 条件1或者条件2
/条件1/&&/条件2/ 条件1并且条件2

[root@docker3 mnt]# awk -F : 'BEGIN{print "yan"}{print $5}END{print "end"}' passwd 
yan
root
bin
daemon
halt
operator
end

显示文件有多少行

[root@docker3 mnt]# awk -F : 'BEGIN{N=0}{N++}END{print N}' passwd 
11

条件可以加多个,用;分开

[root@docker3 mnt]# awk -F : 'BEGIN{N=0}/bash/{N++;print $7}END{print N}' passwd 
/bin/bash
/bin/bash
2
[root@docker3 mnt]# awk -F : 'BEGIN{N=0}/bash/{N++;print $7}END{print N}' passwd 
/bin/bash
/bin/bash
2
[root@docker3 mnt]# awk -F : 'BEGIN{N=0}/bash/{N++;print $0}END{print N}' passwd 
root:x:0:0:root:/root:/bin/bash
yan:x:1000:1000::/home/yan:/bin/bash
2

反选!

[root@docker3 mnt]# awk -F : 'BEGIN{N=0}!/bash/{N++;print $0}END{print N}' passwd 
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
9

多条件

[root@docker3 mnt]# awk -F : 'BEGIN{N=0}/bash|nologin/{N++;print $0}END{print N}' 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
ndbox the tcsd daemon:/dev/null:/sbin/nologin
yan:x:1000:1000::/home/yan:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
10
[root@docker3 mnt]# awk -F : 'BEGIN{N=0}/^root/&&/bash$/{N++;print $0}END{print N}' passwd 
root:x:0:0:root:/root:/bin/bash
1

NR表示行数

[root@docker3 mnt]# awk -F : 'NR==1{print $0}' passwd 
root:x:0:0:root:/root:/bin/bash
[root@docker3 mnt]# awk -F : 'NR==2{print $0}' passwd 
bin:x:1:1:bin:/bin:/sbin/nologin
[root@docker3 mnt]# awk -F : '{print NR}' passwd 
1
2
3
4
5
6
7
8
9
10
11

NF表示列数

[root@docker3 mnt]# awk -F : '{print NF}' passwd 
7
7
7
7
7
7
7
7
7
7
7
[root@docker3 mnt]# awk -F : '{print FILENAME}' passwd 
passwd
passwd
passwd
passwd
passwd
passwd
passwd
passwd
passwd
passwd
passwd

“ ”双引号,表示字符串本身
没有加双引号表示字符串的变量

[root@docker3 mnt]# awk -F : 'BEGIN{YAN=1}{print YAN}' passwd 
1
1
1
1
1
1
1
1
1
1
1
[root@docker3 mnt]# awk -F : 'BEGIN{YAN=1}{print "YAN"}' passwd 
YAN
YAN
YAN
YAN
YAN
YAN
YAN
YAN
YAN
YAN
YAN

~表示是

[root@docker3 mnt]# awk -F : '$7~/bash/{print $0}' passwd 
root:x:0:0:root:/root:/bin/bash
yan:x:1000:1000::/home/yan:/bin/bash

!~不是

[root@docker3 mnt]# awk -F : '$7!~/bash/{print $0}' passwd 
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

awk结合命令

[root@docker3 mnt]# awk '{print "date"}' passwd 
date
date
date
date
date
date
date
date
date
date
date
[root@docker3 mnt]# awk '{print "date"}' passwd | bash
Sun Feb 20 05:22:11 CST 2022
Sun Feb 20 05:22:11 CST 2022
Sun Feb 20 05:22:11 CST 2022
Sun Feb 20 05:22:11 CST 2022
Sun Feb 20 05:22:11 CST 2022
Sun Feb 20 05:22:11 CST 2022
Sun Feb 20 05:22:11 CST 2022
Sun Feb 20 05:22:11 CST 2022
Sun Feb 20 05:22:11 CST 2022
Sun Feb 20 05:22:11 CST 2022
Sun Feb 20 05:22:11 CST 2022

二、 awk用法练习

/etc/passwd文件的第六列没有home关键字并且以bash结尾的行

[root@docker3 mnt]# awk -F : '$6!~/home/&&/bash$/{print}' passwd 
root:x:0:0:root:/root:/bin/bash

统计在系统中能用su切换的并且用户家目录不在/home下的用户数量

错的

[root@docker3 mnt]# awk -F : 'BEGIN{N=0}$7~/bash/&&$6!~/^home/{N++;print $0}END{print N}' passwd 
root:x:0:0:root:/root:/bin/bash
yan:x:1000:1000::/home/yan:/bin/bash
2

应该是3个,useradd -d 指定家目录

[root@docker3 mnt]# useradd -d /mnt/test -s /bin/sh yan2
[root@docker3 mnt]# su - yan2
-sh-4.2$ pwd
/mnt/test
[root@docker3 mnt]# awk -F : 'BEGIN{N=0}$7~/bash/&&$6!~/^home/{N++;print $0}END{print N}' passwd 
root:x:0:0:root:/root:/bin/bash
yan:x:1000:1000::/home/yan:/bin/bash
2
useradd -d /mnt/test -s /bin/sh yan2
[kiosk@foundation38 Desktop]$ awk -F : 'BEGIN{N=0}$7~/bash¥|\<sh\>¥/ &&$6!~/^home/{N++;print $0}END{print N}' passwd 
root:x:0:0:root:/root:/bin/bash
yan:x:1000:1000::/home/yan:/bin/bash
yan2:x:1002:1002::/mnt/test:/bin/sh
3

三、书写脚本通过用户文件建立用户

写个脚本
写个用户文件,启动脚本就能使得用户文件写的用户创建出来

[root@docker3 mnt]# cat userlist 
y1
w2
q3

在这里插入图片描述

[root@docker3 mnt]# sh user_create.sh /mnt
/mnt is not exist or /mnt is not file
[root@docker3 mnt]# sh user_create.sh userlist 
[root@docker3 mnt]# cat /etc/passwd 
y1:x:1003:1003::/home/y1:/bin/bash
w2:x:1004:1004::/home/w2:/bin/bash
q3:x:1005:1005::/home/q3:/bin/bash

xargs使输出通过管道变成输入的时候,成为命令的操作对象而不是参数

[root@docker3 mnt]# ls | cat
user_create.sh
userlist
[root@docker3 mnt]# ls | xargs cat
#!/bin/bash
[ -e "$*" -a -f "$*" ] ||{ 
  echo "$*" is not exist or $* is not file
  exit
}
awk '{print "useradd " $0}' $* | bash
y2
w3
q4
[root@docker3 mnt]# ls | rm -fr
[root@docker3 mnt]# ls
user_create.sh  userlist
[root@docker3 mnt]# ls | xargs rm -fr
[root@docker3 mnt]# ls
[root@docker3 mnt]# 

END

系列文章目录

shell脚本基础知识3

shell脚本基础知识3


一、什么是shell

脚本中命令的解释器
c描述性语言—开发工具 c->os code 需要编译
shell python解释型语言
在这里插入图片描述


二、shell脚本意义

a.记录命令执行的过程和执行逻辑,以便以后重复执行
b.脚本可以批量处理主机
c.脚本可以定时处理主机


三、如何创建shell脚本(幻数)

#!/bin/bash 幻数(不变的量,代码优先执行的程序)类似脚本运行的依赖性

/mnt/test.sh ctrl+z打入后台,执行脚本的时候运行了幻数

[root@docker3 mnt]# cat test.sh 
#!/bin/bash
cat
[root@docker3 mnt]# chmod +x test.sh 
[root@docker3 mnt]# /mnt/test.sh 
^Z
[1]+  Stopped                 /mnt/test.sh
[root@docker3 mnt]# ps f 
  PID TTY      STAT   TIME COMMAND
 3482 pts/0    Ss     0:00 -bash
 4572 pts/0    T      0:00  \_ /bin/bash /mnt/test.sh
 4573 pts/0    T      0:00  |   \_ cat
 4579 pts/0    R+     0:00  \_ ps f
 3021 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

更改幻数
bash变成sh了

[root@docker3 mnt]# cat test.sh 
#!/bin/sh
cat
[root@docker3 mnt]# /mnt/test.sh 
^Z
[2]+  Stopped                 /mnt/test.sh
[root@docker3 mnt]# ps f 
  PID TTY      STAT   TIME COMMAND
 3482 pts/0    Ss     0:00 -bash
 4572 pts/0    T      0:00  \_ /bin/bash /mnt/test.sh
 4573 pts/0    T      0:00  |   \_ cat
 4589 pts/0    T      0:00  \_ /bin/sh /mnt/test.sh
 4590 pts/0    T      0:00  |   \_ cat
 4591 pts/0    R+     0:00  \_ ps f
 3021 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

可以跟你想要的环境,vim不行,vim是交互式

[root@docker3 mnt]# cat test.sh 
#!/bin/cat
afsdf
afsaf
af
[root@docker3 mnt]# /mnt/test.sh 
#!/bin/cat
afsdf
afsaf
af

四、自动生成脚本头信息

每次写脚本添加这些信息很麻烦
在这里插入图片描述
所以可以配置vim的生成模板
修改这个文件后,任何人使用vim,都会生成模板

[root@docker3 mnt]# vim /etc/vimrc 

所以编写自己用户的vim的配置文件
ts是一个tab长度,sw缩进的长度,ai自动缩进,et把tab键自动转化成空格,不加et,tab一次2个空格是一个整体,加上后,tab一次是一个空格,一共两个空格

[root@docker3 mnt]# vim ~/.vimrc

按键添加,append(0)是第一行

setloacl ts=2 sw=2 ai et
map <F9> ms:call SHELLTITLE()<cr>'s
"autocmd BufNewFile *.sh call SHELLTITLE()"
func SHELLTITLE()
 call append(0,"#########################################")
endfunc

自动挂载,新文件sh结尾的文件

setloacl ts=2 sw=2 ai et
"map <F9> ms:call SHELLTITLE()<cr>'s
autocmd BufNewFile *.sh call SHELLTITLE()"
func SHELLTITLE()
 call append(0,"#########################################")
endfunc

自动添加上去了

[root@docker3 mnt]# vim test3.sh
#########################################

.表示这段结束,下一段
strftime采集时间的函数%Y年%m月%d天

setloacl ts=2 sw=2 ai et
autocmd BufNewFile *.sh call SHELLTITLE()"
func SHELLTITLE()
 call append(0,"#########################################")
  call append(1,"# Create_Time".strftime("%Y%m%d"))
endfunc

五、shell脚本运行方式

5.1手动在环境中开启指定解释器,不会开启脚本指定的幻数

[root@docker3 mnt]# vim test.sh
[root@docker3 mnt]# sh test.sh 
^Z
[1]+  Stopped                 sh test.sh
[root@docker3 mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 3486 pts/0    Ss     0:00 -bash
 3577 pts/0    T      0:00  \_ sh test.sh
 3578 pts/0    T      0:00  |   \_ cat
 3579 pts/0    R+     0:00  \_ ps f
 3006 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

5.2不会开启一个新的sh,在当前环境运行sh,当前环境bash

[root@docker3 mnt]# . test2.sh.sh 
[root@docker3 mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 3486 pts/0    Ss     0:00 -bash
 3627 pts/0    T      0:00  \_ cat
 3628 pts/0    R+     0:00  \_ ps f
 3622 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

.空格加脚本=source 脚本

[root@docker3 mnt]# fg
cat
^C
[root@docker3 mnt]# source test2.sh 
^Z
[1]+  Stopped                 cat
[root@docker3 mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 3486 pts/0    Ss     0:00 -bash
 3631 pts/0    T      0:00  \_ cat
 3632 pts/0    R+     0:00  \_ ps f
 3622 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

加入可执行权限后,脚本会调用幻数执行


5.3开启脚本中指定的shell并使用此shell环境运行脚本的指令

./脚本代表当前环境下开启或者绝对路径方式调用,必须加执行权限

[root@docker3 mnt]# ls -l test2.sh 
-rw-r--r-- 1 root root 14 Feb 20 21:46 test2.sh
[root@docker3 mnt]# chmod +x test2.sh 
[root@docker3 mnt]# ls -l test2.sh 
-rwxr-xr-x 1 root root 14 Feb 20 21:46 test2.sh
[root@docker3 mnt]# ./test2.sh 
^Z
[1]+  Stopped                 ./test2.sh
[root@docker3 mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 3486 pts/0    Ss     0:00 -bash
 3642 pts/0    T      0:00  \_ /bin/sh ./test2.sh
 3643 pts/0    T      0:00  |   \_ cat
 3645 pts/0    R+     0:00  \_ ps f
 3622 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

相当于绝对路径方式

[root@docker3 mnt]# /mnt/test2.sh 
^Z
[1]+  Stopped                 /mnt/test2.sh
[root@docker3 mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 3486 pts/0    Ss     0:00 -bash
 3648 pts/0    T      0:00  \_ /bin/sh /mnt/test2.sh
 3649 pts/0    T      0:00  |   \_ cat
 3650 pts/0    R+     0:00  \_ ps f
 3622 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

六、对脚本的调试

脚本卡死,不动

[root@docker3 mnt]# cat test.sh 
#!/bin/sh
date
cal
cat
ls /mnt
[root@docker3 mnt]# sh test.sh 
Sun Feb 20 21:59:09 CST 2022
    February 2022   
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

sh -x 对脚本执行的检测,带+表示执行的动作,不带加号代表执行的输出,可以看出问题点在cat

[root@docker3 mnt]# sh -x test.sh 
+ date
Sun Feb 20 22:00:36 CST 2022
+ cal
    February 2022   
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

+ cat


七、书写清空日志脚本

脚本:
clear_log.sh 执行脚本后可以清空日志
里面RULES告诉了日志的采集点,所以我们要过滤RULES底下的行

[kiosk@foundation38 Desktop]$ cat /etc/rsyslog.conf 
[root@docker3 mnt]# grep -A $(sed -n '$=' /etc/rsyslog.conf) RULES /etc/rsyslog.conf 

这个写法

[root@docker3 mnt]# sed '/^#/d' /etc/rsyslog.conf | sed '/^$/d;/^\$/d;s/-/ /g;/:omusrmsg:/d' |  awk {'print $2'}  

或者这样写

[root@docker3 mnt]# grep -A $(sed -n '$=' /etc/rsyslog.conf) RULES /etc/rsyslog.conf | sed 's/-//g;/^$/d;/^#/d;/:omusrmsg:/d'| awk '{print $2}'
/var/log/messages
/var/log/secure
/var/log/maillog
/var/log/cron
/var/log/spooler
/var/log/boot.log

清空

[root@docker3 mnt]# grep -A $(sed -n '$=' /etc/rsyslog.conf) RULES /etc/rsyslog.conf | sed 's/-//g;/^$/d;/^#/d;/:omusrmsg:/d'| awk '{print ">" $2}' 
>/var/log/messages
>/var/log/secure
>/var/log/maillog
>/var/log/cron
>/var/log/spooler
>/var/log/boot.log
[root@docker3 mnt]# grep -A $(sed -n '$=' /etc/rsyslog.conf) RULES /etc/rsyslog.conf | sed 's/-//g;/^$/d;/^#/d;/:omusrmsg:/d'| awk '{print ">" $2}' | bash
[root@docker3 mnt]# cat /var/log/messages
[root@docker3 mnt]# systemctl restart sshd
[root@docker3 mnt]# cat /var/log/messages
Feb 20 22:42:59 docker3 systemd: Stopping OpenSSH server daemon...
Feb 20 22:42:59 docker3 systemd: Stopped OpenSSH server daemon.
Feb 20 22:42:59 docker3 systemd: Starting OpenSSH server daemon...
Feb 20 22:42:59 docker3 systemd: Started OpenSSH server daemon.
[root@docker3 mnt]# grep -A $(sed -n '$=' /etc/rsyslog.conf) RULES /etc/rsyslog.conf | sed 's/-//g;/^$/d;/^#/d;/:omusrmsg:/d'| awk '{print ">" $2}' | bash
[root@docker3 mnt]# cat /var/log/messages

END

系列文章目录

shell脚本基础知识3

shell脚本基础知识3


一、什么是shell

脚本中命令的解释器
c描述性语言—开发工具 c->os code 需要编译
shell python解释型语言
在这里插入图片描述


二、shell脚本意义

a.记录命令执行的过程和执行逻辑,以便以后重复执行
b.脚本可以批量处理主机
c.脚本可以定时处理主机


三、如何创建shell脚本(幻数)

#!/bin/bash 幻数(不变的量,代码优先执行的程序)类似脚本运行的依赖性

/mnt/test.sh ctrl+z打入后台,执行脚本的时候运行了幻数

[root@docker3 mnt]# cat test.sh 
#!/bin/bash
cat
[root@docker3 mnt]# chmod +x test.sh 
[root@docker3 mnt]# /mnt/test.sh 
^Z
[1]+  Stopped                 /mnt/test.sh
[root@docker3 mnt]# ps f 
  PID TTY      STAT   TIME COMMAND
 3482 pts/0    Ss     0:00 -bash
 4572 pts/0    T      0:00  \_ /bin/bash /mnt/test.sh
 4573 pts/0    T      0:00  |   \_ cat
 4579 pts/0    R+     0:00  \_ ps f
 3021 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

更改幻数
bash变成sh了

[root@docker3 mnt]# cat test.sh 
#!/bin/sh
cat
[root@docker3 mnt]# /mnt/test.sh 
^Z
[2]+  Stopped                 /mnt/test.sh
[root@docker3 mnt]# ps f 
  PID TTY      STAT   TIME COMMAND
 3482 pts/0    Ss     0:00 -bash
 4572 pts/0    T      0:00  \_ /bin/bash /mnt/test.sh
 4573 pts/0    T      0:00  |   \_ cat
 4589 pts/0    T      0:00  \_ /bin/sh /mnt/test.sh
 4590 pts/0    T      0:00  |   \_ cat
 4591 pts/0    R+     0:00  \_ ps f
 3021 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

可以跟你想要的环境,vim不行,vim是交互式

[root@docker3 mnt]# cat test.sh 
#!/bin/cat
afsdf
afsaf
af
[root@docker3 mnt]# /mnt/test.sh 
#!/bin/cat
afsdf
afsaf
af

四、自动生成脚本头信息

每次写脚本添加这些信息很麻烦
在这里插入图片描述
所以可以配置vim的生成模板
修改这个文件后,任何人使用vim,都会生成模板

[root@docker3 mnt]# vim /etc/vimrc 

所以编写自己用户的vim的配置文件
ts是一个tab长度,sw缩进的长度,ai自动缩进,et把tab键自动转化成空格,不加et,tab一次2个空格是一个整体,加上后,tab一次是一个空格,一共两个空格

[root@docker3 mnt]# vim ~/.vimrc

按键添加,append(0)是第一行

setloacl ts=2 sw=2 ai et
map <F9> ms:call SHELLTITLE()<cr>'s
"autocmd BufNewFile *.sh call SHELLTITLE()"
func SHELLTITLE()
 call append(0,"#########################################")
endfunc

自动挂载,新文件sh结尾的文件

setloacl ts=2 sw=2 ai et
"map <F9> ms:call SHELLTITLE()<cr>'s
autocmd BufNewFile *.sh call SHELLTITLE()"
func SHELLTITLE()
 call append(0,"#########################################")
endfunc

自动添加上去了

[root@docker3 mnt]# vim test3.sh
#########################################

.表示这段结束,下一段
strftime采集时间的函数%Y年%m月%d天

setloacl ts=2 sw=2 ai et
autocmd BufNewFile *.sh call SHELLTITLE()"
func SHELLTITLE()
 call append(0,"#########################################")
  call append(1,"# Create_Time".strftime("%Y%m%d"))
endfunc

五、shell脚本运行方式

5.1手动在环境中开启指定解释器,不会开启脚本指定的幻数

[root@docker3 mnt]# vim test.sh
[root@docker3 mnt]# sh test.sh 
^Z
[1]+  Stopped                 sh test.sh
[root@docker3 mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 3486 pts/0    Ss     0:00 -bash
 3577 pts/0    T      0:00  \_ sh test.sh
 3578 pts/0    T      0:00  |   \_ cat
 3579 pts/0    R+     0:00  \_ ps f
 3006 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

5.2不会开启一个新的sh,在当前环境运行sh,当前环境bash

[root@docker3 mnt]# . test2.sh.sh 
[root@docker3 mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 3486 pts/0    Ss     0:00 -bash
 3627 pts/0    T      0:00  \_ cat
 3628 pts/0    R+     0:00  \_ ps f
 3622 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

.空格加脚本=source 脚本

[root@docker3 mnt]# fg
cat
^C
[root@docker3 mnt]# source test2.sh 
^Z
[1]+  Stopped                 cat
[root@docker3 mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 3486 pts/0    Ss     0:00 -bash
 3631 pts/0    T      0:00  \_ cat
 3632 pts/0    R+     0:00  \_ ps f
 3622 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

加入可执行权限后,脚本会调用幻数执行


5.3开启脚本中指定的shell并使用此shell环境运行脚本的指令

./脚本代表当前环境下开启或者绝对路径方式调用,必须加执行权限

[root@docker3 mnt]# ls -l test2.sh 
-rw-r--r-- 1 root root 14 Feb 20 21:46 test2.sh
[root@docker3 mnt]# chmod +x test2.sh 
[root@docker3 mnt]# ls -l test2.sh 
-rwxr-xr-x 1 root root 14 Feb 20 21:46 test2.sh
[root@docker3 mnt]# ./test2.sh 
^Z
[1]+  Stopped                 ./test2.sh
[root@docker3 mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 3486 pts/0    Ss     0:00 -bash
 3642 pts/0    T      0:00  \_ /bin/sh ./test2.sh
 3643 pts/0    T      0:00  |   \_ cat
 3645 pts/0    R+     0:00  \_ ps f
 3622 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

相当于绝对路径方式

[root@docker3 mnt]# /mnt/test2.sh 
^Z
[1]+  Stopped                 /mnt/test2.sh
[root@docker3 mnt]# ps f
  PID TTY      STAT   TIME COMMAND
 3486 pts/0    Ss     0:00 -bash
 3648 pts/0    T      0:00  \_ /bin/sh /mnt/test2.sh
 3649 pts/0    T      0:00  |   \_ cat
 3650 pts/0    R+     0:00  \_ ps f
 3622 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux

六、对脚本的调试

脚本卡死,不动

[root@docker3 mnt]# cat test.sh 
#!/bin/sh
date
cal
cat
ls /mnt
[root@docker3 mnt]# sh test.sh 
Sun Feb 20 21:59:09 CST 2022
    February 2022   
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

sh -x 对脚本执行的检测,带+表示执行的动作,不带加号代表执行的输出,可以看出问题点在cat

[root@docker3 mnt]# sh -x test.sh 
+ date
Sun Feb 20 22:00:36 CST 2022
+ cal
    February 2022   
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

+ cat


七、书写清空日志脚本

脚本:
clear_log.sh 执行脚本后可以清空日志
里面RULES告诉了日志的采集点,所以我们要过滤RULES底下的行

[kiosk@foundation38 Desktop]$ cat /etc/rsyslog.conf 
[root@docker3 mnt]# grep -A $(sed -n '$=' /etc/rsyslog.conf) RULES /etc/rsyslog.conf 

这个写法

[root@docker3 mnt]# sed '/^#/d' /etc/rsyslog.conf | sed '/^$/d;/^\$/d;s/-/ /g;/:omusrmsg:/d' |  awk {'print $2'}  

或者这样写

[root@docker3 mnt]# grep -A $(sed -n '$=' /etc/rsyslog.conf) RULES /etc/rsyslog.conf | sed 's/-//g;/^$/d;/^#/d;/:omusrmsg:/d'| awk '{print $2}'
/var/log/messages
/var/log/secure
/var/log/maillog
/var/log/cron
/var/log/spooler
/var/log/boot.log

清空

[root@docker3 mnt]# grep -A $(sed -n '$=' /etc/rsyslog.conf) RULES /etc/rsyslog.conf | sed 's/-//g;/^$/d;/^#/d;/:omusrmsg:/d'| awk '{print ">" $2}' 
>/var/log/messages
>/var/log/secure
>/var/log/maillog
>/var/log/cron
>/var/log/spooler
>/var/log/boot.log
[root@docker3 mnt]# grep -A $(sed -n '$=' /etc/rsyslog.conf) RULES /etc/rsyslog.conf | sed 's/-//g;/^$/d;/^#/d;/:omusrmsg:/d'| awk '{print ">" $2}' | bash
[root@docker3 mnt]# cat /var/log/messages
[root@docker3 mnt]# systemctl restart sshd
[root@docker3 mnt]# cat /var/log/messages
Feb 20 22:42:59 docker3 systemd: Stopping OpenSSH server daemon...
Feb 20 22:42:59 docker3 systemd: Stopped OpenSSH server daemon.
Feb 20 22:42:59 docker3 systemd: Starting OpenSSH server daemon...
Feb 20 22:42:59 docker3 systemd: Started OpenSSH server daemon.
[root@docker3 mnt]# grep -A $(sed -n '$=' /etc/rsyslog.conf) RULES /etc/rsyslog.conf | sed 's/-//g;/^$/d;/^#/d;/:omusrmsg:/d'| awk '{print ">" $2}' | bash
[root@docker3 mnt]# cat /var/log/messages

END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值