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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值