shell详解

系列文章目录

shell脚本基础知识3

shell脚本基础知识3

  • 系列文章目录
  • 一、什么是shell
  • 二、shell脚本意义
  • 三、如何创建shell脚本(幻数)
  • 四、自动生成脚本头信息
  • 五、shell脚本运行方式
    • 5.1手动在环境中开启指定解释器,不会开启脚本指定的幻数
    • 5.2不会开启一个新的sh,在当前环境运行sh,当前环境bash
    • 5.3开启脚本中指定的shell并使用此shell环境运行脚本的指令
  • 六、对脚本的调试
  • 七、书写清空日志脚本


一、什么是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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/448413.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

基于 Win Server 2008 复现 IPC$ 漏洞

写在前面 本篇博客演示了使用 winXP&#xff08;配合部分 win10 的命令&#xff09;对 win server 2008 的 IPC$ 漏洞进行内网渗透&#xff0c;原本的实验是要求使用 win server 2003&#xff0c;使用 win server 2003 可以规避掉很多下面存在的问题&#xff0c;建议大家使用 …

【论文阅读】Generative Pretraining from Pixels

Generative Pretraining From Pixels 引用&#xff1a; Chen M, Radford A, Child R, et al. Generative pretraining from pixels[C]//International conference on machine learning. PMLR, 2020: 1691-1703. 论文链接&#xff1a; http://proceedings.mlr.press/v119/chen…

仿牛客网项目---消息队列的实现

本篇文章讲一讲我们的项目中用到的消息队列。 1.阻塞队列 2.kafka 我的项目为什么要用消息队列&#xff1f; 如果采用消息队列&#xff0c;那么评论、点赞、关注三类不同的事&#xff0c;可以定义三类不同的主题&#xff08;评论、点赞、关注&#xff09;&#xff0c;发生相应…

Makedown语法

这里写自定义目录标题 欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants 创建一个自定义列表如何创建一个…

考研C语言复习初阶(5)

目录 一.表达式求值 1.1隐式类型转换 1.2 算术转换 12.3 操作符的属性 二. 指针是什么&#xff1f; 三 指针和指针类型 3.1 指针-整数 3.2 指针的解引用 3.3 野指针 四.指针运算 4.1 指针-整数 4.2 指针-指针 4.3 指针的关系运算 5. 指针和数组 6. 二级指针 …

【学习笔记】VMware vSphere 6.7虚拟化入门

VMware vSphere 6.7虚拟化入门课程介绍 课程内容 1、VMware vSphere 6.7虚拟化入门课程介绍 2、ESXi6.7控制台设置 3、使用vSpkere Host client管理虚拟机 4、VMware EsXi基础操作 5、VMware Esxi存储管理 6、管理ESXi主机网络与虚拟机网络 7、安装配置vCenter Server Applia…

哈希表|1.两数之和

力扣题目链接 /*** Note: The returned array must be malloced, assume caller calls free().*/// leetcode 支持 ut_hash 函式庫typedef struct {int key;int value;UT_hash_handle hh; // make this structure hashable} map;map* hashMap NULL;void hashMapAdd(int key, i…

【JAVA类】利用接口的多继承实现———图书管理系统【附源码】

引言 在我们学习了一些java的基础语法之后&#xff0c;需要把这些知识点可以串起来&#xff0c;这里使用一个简单的小项目可以很好的帮助我们牢记这些知识点&#xff0c;今天就带大家学习一个有关java的小项目&#xff0c;很多学校也经常把这个项目作为他们的课程设计——经典的…

HTML5+CSS3小实例:按钮边框动效

实例:按钮边框动效 技术栈:HTML+CSS 效果: 源码: 【HTML】 <!DOCTYPE html> <html lang="zh-CN"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.…

git入门到精通

第3章 Git常用命令 3.1 设置用户签名 3.2 初始化本地库 3.3 查看本地 状态 3.3.1 首次查看&#xff08;工作区没有任何文件&#xff09; 3.3.2 新增文件&#xff08;hello.txt&#xff09; 3.3.3 再次查者&#xff08;检測到末追踪的文件&#xff09; 3.4添加暫存区 3…

System Verilog学习笔记(二十)——TCL基础

TCL简介 TCL&#xff08;Tool Command Language&#xff09;是一种解释执行的脚本语言。它提供了通用的编程能力&#xff1a;支持变量、过程和控制结构&#xff1b;同时TCL还拥有功能强大的固有的核心命令集 由于TCL的解释器是用C/C语言的过程库实现的&#xff0c;因此可以把T…

Unity笔记:C#基础(1)

杂项 虚函数 CSDN - C虚函数详解 cnblog - C#中的虚函数virtual 常量池与new 在C#中&#xff0c;string是不可变的&#xff0c;这意味着对string对象的操作通常会返回一个新的string对象&#xff0c;而不会修改原始的string对象。因此&#xff0c;几乎所有涉及更改string内…

现在可以在Mac桌面上快速打开C知道进行AI提问

看&#xff0c;我的Mac桌面有个C知道组件&#xff0c;点击即可快速打开C知道页面进行提问使用&#xff0c;再也不需要先打开浏览器&#xff0c;再输入csdn.net访问网站&#xff0c;然后点击页面上的C知道入口。 这是如何实现的呢&#xff1f; 首先&#xff0c;我们来做一些准…

微信聊天助手

最近我在负责公司的招聘工作时&#xff0c;真是深感繁琐与困扰。每当新添加求职者的微信&#xff0c;为了了解他们的基本情况以及求职意向&#xff0c;不得不反复地提出一系列固定的问题。比如询问他们的教育背景、工作经验、擅长技能、期望职位等等&#xff0c;这些必不可少却…

东京工业大学最新!一种具有多周期特征描述的精确ORB提取器

作者&#xff1a;小柠檬 | 来源&#xff1a;3DCV 在公众号「3DCV」后台&#xff0c;回复「原论文」可获取论文pdf 添加微信&#xff1a;dddvision&#xff0c;备注&#xff1a;3D高斯&#xff0c;拉你入群。文末附行业细分群 详细内容请关注3DCV 3D视觉精品课程&#xff1a;…

【每日一题】2834. 找出美丽数组的最小和-2024.3.8

题目&#xff1a; 2834. 找出美丽数组的最小和 给你两个正整数&#xff1a;n 和 target 。 如果数组 nums 满足下述条件&#xff0c;则称其为 美丽数组 。 nums.length n.nums 由两两互不相同的正整数组成。在范围 [0, n-1] 内&#xff0c;不存在 两个 不同 下标 i 和 j &…

P5266 【深基17.例6】学籍管理题解

题目 您要设计一个学籍管理系统&#xff0c;最开始学籍数据是空的&#xff0c;然后该系统能够支持下面的操作&#xff08;不超过条&#xff09;&#xff1a; 插入与修改&#xff0c;格式1 NAME SCORE&#xff1a;在系统中插入姓名为NAME(由字母和数字组成不超过20个字符的字符…

博客系统(SSM)

前端页面http://t.csdnimg.cn/zwKyG以上是之前写过的博客前端页面的内容&#xff0c;下面是通过SSM实现的后端内容。 目录 一.准备工作 1.1数据准备 1.2修改配置版本文件 1.3配置数据库 二.项目公共模块 2.1实体类 2.2操作数据库部分 三.功能开发 3.1博客列表 获取博…

【2024】使用Vuetifi搭建vue3+Ts项目,并使用tailwind.css

目录 使用Vuetifi搭建项目使用tailwind.css 只要跟着官方文档来就不会出错。 使用Vuetifi搭建项目 npm create vuetifyyarn create vuetifypnpm create vuetifybun create vuetify在终端运行一个就行&#xff0c;之后就可以选配置了。 使用tailwind.css 先运行&#xff1a; …

最大的单入口空闲区域

最大的单入口空闲区域 问题描述输入输出代码实现 问题描述 找到最大的单入口空闲区域。 空闲区域是由连通的’O’组成的区域&#xff0c;位于边界的’O’可以是入口&#xff0c; 单入口空闲区域即有且只有一个位于边界的’O’作为入口的由连通的’O’组成的区域。 如果两个元素…
最新文章