运维Shell脚本牛刀小试(九): 重定向操作符“>“及双重定向“>>“

news2024/10/7 16:18:35


运维Shell脚本小试牛刀(一)

运维Shell脚本小试牛刀(二)

运维Shell脚本小试牛刀(三)::$(cd $(dirname $0); pwd)命令详解

运维Shell脚本小试牛刀(四): 多层嵌套if...elif...elif....else fi_蜗牛杨哥的博客-CSDN博客

Cenos7安装小火车程序动画

运维Shell脚本小试牛刀(五):until循环

运维Shell脚本小试牛刀(六): Shell中的函数认知

运维Shell脚本小试牛刀(七):从函数文件中调用另外一个脚本文件中函数

运维Shell脚本小试牛刀(八): case模式忽略命令行参数大小写演示

运维Shell脚本牛刀小试(九): 重定向操作符“>“及双重定向“>>“


一:  重定向操作符到文件

">"单重定向操作符  ,如该例,循环给定目录中的以".tmp"结尾的文件,循环删除,如果删除过程中发生错误,则把该错误信息输入到errors.log文件;

[root@www standandinout]# cat removetmp.sh 
#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  removetmp.sh
#                           USAGE: ./removetmp.sh
#    DESCRIPTION:  查找给定目录下的.tmp结尾的危机,并循环删除,如果循环遇到错误则重定向到errors.log文件
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 如果参数个数小于1,(没有指定参数),则执行
if [ $# -lt 1 ]; then

# 打印脚本的使用的方法信息
   echo "Usage: $0 DIRECTORY............."
   # 退出脚本
   exit;

fi

# 使用for循环变量在命令行给脚本指定的所有参数

for dir in $@
  do
     
    # 找到指定目录中以.tmp为后缀的文件,并将其删除
    find $dir -name "*.tmp" -exec rm -f {} \;
    # 将for循环产生的错误信息写入到文件errors.log
   done 2>errors.log
 

二: 输出测试


[root@www standandinout]# ll
总用量 8
-rw-r--r-- 1 root root   61 9月  11 19:54 date.txt
-rw-r--r-- 1 root root    0 9月  11 21:22 errors.log
-rwxr-xr-x 1 root root 1255 9月  11 21:17 removetmp.sh

创建 date.tmp errors.tmp  first.tmp 文件
[root@www standandinout]# touch date.tmp errors.tmp  first.tmp
[root@www standandinout]# ll   
总用量 8
-rw-r--r-- 1 root root    0 9月  11 21:32 date.tmp
-rw-r--r-- 1 root root   61 9月  11 19:54 date.txt
-rw-r--r-- 1 root root    0 9月  11 21:22 errors.log
-rw-r--r-- 1 root root    0 9月  11 21:32 errors.tmp
-rw-r--r-- 1 root root    0 9月  11 21:32 first.tmp
-rwxr-xr-x 1 root root 1255 9月  11 21:17 removetmp.sh


执行命令:

[root@www standandinout]# ./removetmp.sh 
Usage: ./removetmp.sh DIRECTORY.............
[root@www standandinout]# pwd
/usr/local/example/standandinout
[root@www standandinout]# ./removetmp.sh  /usr/local/example/standandinout

# 执行该命令后,"*.tmp"结尾的文件被删除
[root@www standandinout]# ll
总用量 8
-rw-r--r-- 1 root root   61 9月  11 19:54 date.txt
-rw-r--r-- 1 root root    0 9月  11 21:34 errors.log
-rwxr-xr-x 1 root root 1255 9月  11 21:17 removetmp.sh


[root@www standandinout]# touch first.tmp second.tmp three.tmp 
[root@www standandinout]# ll
总用量 8
-rw-r--r-- 1 root root    0 9月  11 21:46 date.tmp
-rw-r--r-- 1 root root   61 9月  11 19:54 date.txt
-rw-r--r-- 1 root root    0 9月  11 21:34 errors.log
-rw-r--r-- 1 root root    0 9月  11 21:47 first.tmp
-rwxr-xr-x 1 root root 1255 9月  11 21:17 removetmp.sh
-rw-r--r-- 1 root root    0 9月  11 21:47 second.tmp
-rw-r--r-- 1 root root    0 9月  11 21:47 three.tmp
[root@www standandinout]# ./removetmp.sh /usr/local/example/standandinout /usr/local/example/
[root@www standandinout]# ll
总用量 8
-rw-r--r-- 1 root root   61 9月  11 19:54 date.txt
-rw-r--r-- 1 root root    0 9月  11 21:48 errors.log
-rwxr-xr-x 1 root root 1255 9月  11 21:17 removetmp.sh
 

三: 构造异常执行条件 


由于当前操作用户为root,为异常信息数据重定向到errors.log文件中,现添加一个yangge用户及组

分别执行:


useradd yangge; # 添加用户yangge

groupadd yangge;  # 添加yangge组

使用root用户创建 touch first.tmp  second.tmp date.tmp文件,

更改errors.log与removetmp.sh所属用户及用户组


[yangge@www standandinout]$ chown yangge:yangge removetmp.sh errors.log 
[yangge@www standandinout]$ ll
总用量 12
-rw-r--r-- 1 root   root      0 9月  11 22:35 date.tmp
-rw-r--r-- 1 root   root     61 9月  11 19:54 date.txt
-rw-r--r-- 1 yangge yangge  149 9月  11 22:35 errors.log
-rw-r--r-- 1 root   root      0 9月  11 22:35 first.tmp
-rwxr-xr-x 1 yangge yangge 1255 9月  11 21:17 removetmp.sh
 

四: 切换到yangge账号,执行如下命令 


-rwxr-xr-x 1 yangge yangge 1255 9月  11 21:17 removetmp.sh
[yangge@www standandinout]$ ./removetmp.sh /usr/local/example/standandinout
[yangge@www standandinout]$ ll
总用量 12
-rw-r--r-- 1 root   root      0 9月  11 22:35 date.tmp
-rw-r--r-- 1 root   root     61 9月  11 19:54 date.txt
-rw-r--r-- 1 yangge yangge  149 9月  11 22:35 errors.log
-rw-r--r-- 1 root   root      0 9月  11 22:35 first.tmp
-rwxr-xr-x 1 yangge yangge 1255 9月  11 21:17 removetmp.sh

# 查看错误日志文件,记录了操作总参数的异常,可见单重定向">"数据到错误文件
[yangge@www standandinout]$ cat errors.log 
rm: 无法删除"/usr/local/example/standandinout/date.tmp": 权限不够
rm: 无法删除"/usr/local/example/standandinout/first.tmp": 权限不够


再次执行脚本命令:

[yangge@www standandinout]$ ./removetmp.sh /usr/local/example/standandinout
[yangge@www standandinout]$ ll
总用量 12
-rw-r--r-- 1 root   root      0 9月  11 22:35 date.tmp
-rw-r--r-- 1 root   root     61 9月  11 19:54 date.txt
-rw-r--r-- 1 yangge yangge  149 9月  11 22:47 errors.log
-rw-r--r-- 1 root   root      0 9月  11 22:35 first.tmp
-rwxr-xr-x 1 yangge yangge 1255 9月  11 21:17 removetmp.sh
[yangge@www standandinout]$ cat errors.log 
rm: 无法删除"/usr/local/example/standandinout/date.tmp": 权限不够
rm: 无法删除"/usr/local/example/standandinout/first.tmp": 权限不够

引出问题:

错误日志还是两行,说明每次执行都会清空日志,重新重定向到错误日志文件;当我们需要每次都追加错误日志到文件,有没有解决方案呢? 当然有,使用双重定向操作符">>" 即可每次执行不会清楚先前的日志内容,会在文件下行最近日志到错误文件

 

五: 双重定向操作符实现追加数据到文件

#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  removetmp.sh
#                           USAGE: ./removetmp.sh
#    DESCRIPTION:  查找给定目录下的.tmp结尾的危机,并循环删除,如果循环遇到错误则重定向到errors.log文件
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 如果参数个数小于1,(没有指定参数),则执行
if [ $# -lt 1 ]; then

# 打印脚本的使用的方法信息
   echo "Usage: $0 DIRECTORY............."
   # 退出脚本
   exit;

fi

# 使用for循环变量在命令行给脚本指定的所有参数

for dir in $@
  do

    # 找到指定目录中以.tmp为后缀的文件,并将其删除
    find $dir -name "*.tmp" -exec rm -f {} \;
    # 将for循环产生的错误信息写入到文件errors.log
   done 2>>errors.log

~                                                          

 六: 测试双重定向操作符执行结果

[yangge@www standandinout]$ ./removetmp.sh /usr/local/example/standandinout
[yangge@www standandinout]$ cat errors.log 
rm: 无法删除"/usr/local/example/standandinout/date.tmp": 权限不够
rm: 无法删除"/usr/local/example/standandinout/first.tmp": 权限不够
rm: 无法删除"/usr/local/example/standandinout/date.tmp": 权限不够
rm: 无法删除"/usr/local/example/standandinout/first.tmp": 权限不够
 


可以看出日志文件,多了两条数据。。。。。。。。

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

c高级day4(9.11)shell脚本(case ....in语句,循环语句,select ...in和case...In结合,辅助控制关键字,函数)

1.实现一个对数组就和的函数,数组通过实参传递给函数 2.写一个函数,输出当前用户的uid和gid,并使用变量接收结果 #!/bin/bash read -a arr sum0 function add() { …

s2019nh62分数减法

代码&#xff1a; #include<bits/stdc.h> using namespace std; int m1,z1,m2,z2,zd,zx,s1,s2,f1,f2,c,da; int main() {cin>>z1>>m1; //分子1和分母1cin>>z2>>m2; //分子2和分母2zd__gcd(m1,m2); //求两个分母的最大公因数来求最小公倍数z…

数据分析工具有哪些,哪个好学?

Tableau、帆软BI、思迈特BI、SpeedBI数据分析云……这些都是比较常见的BI数据分析工具。从学习成本、操作难度以及数据可视化分析效果来看&#xff0c;SpeedBI数据分析云都表现地可圈可点。 1、不需下载安装、学习成本低 SpeedBI数据分析云是一款SaaS BI数据分析工具&#xf…

IO和进程day05(进程与线程)

今日任务 1.代码 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <sys/wait.h> #include <pthread.h>…

pytorch代码实现之CoordConv卷积

CoordConv卷积 在深度学习领域&#xff0c;几乎没有什么想法能像卷积那样产生如此大的影响。对于任何涉及像素或空间表示的问题&#xff0c;普遍的直觉认为卷积神经网络可能是合适的。在本文中&#xff0c;我们通过看似平凡的坐标变换问题展示了一个惊人的反例&#xff0c;该问…

如何使用bat脚本启动指定目录下的jar包

士别三日&#xff0c;当刮目相待。——《三国志》 为了将一个java程序封装成一个简单易用的小工具&#xff0c;使用bat脚本启动jar包。 在txt文档中&#xff0c;键入&#xff1a; echo off java -jar %~dp0core\demo.jar 注意&#xff1a; 1、其中“core”是文件夹的名称&am…

与读者互动,扩大影响:提升公众号文章阅读量的关键

如何提升公众号文章阅读量 公众号已成为许多个人和企业推广与传播的重要平台。然而&#xff0c;仅仅拥有一个公众号并发布文章并不足以吸引大量读者和提高阅读量。在当今信息爆炸的时代&#xff0c;如何让你的公众号文章脱颖而出并吸引更多读者的关注是一个关键问题。本文将为…

气象观测站:实时监测、应用广泛

对于许多人来说&#xff0c;气象观测站可能只是一种能够预测天气的设备&#xff0c;但实际上&#xff0c;它所涉及的原理和优势却远不止于此。 二、气象观测站的优势 全面覆盖 气象观测站能够全面覆盖气象要素的各个方面&#xff0c;从温度、湿度、气压到风速、风向等。这些…

日志是你的朋友:为什么每个开发者都应该写日志

大家好&#xff0c;我是小米&#xff0c;一个热衷于技术分享的程序员。今天我想和大家聊一聊一个在编写代码时常常被忽视&#xff0c;却极为重要的话题——为什么要写有意义的日志。 在日常的编程工作中&#xff0c;我们经常听到“日志”这个词&#xff0c;但是有些人可能并不…

C++重载输入和输出运算符

在C++中,标准库本身已经对左移运算符<<和右移运算符>>分别进行了重载,使其能够用于不同数据的输入输出,但是输入输出的对象只能是 C++ 内置的数据类型(例如 bool、int、double 等)和标准库所包含的类类型(例如 string、complex、ofstream、ifstream 等)。 …

C 风格文件输入/输出---无格式输入/输出---(std::getchar,std::putchar,std::ungetc)

C 标准库的 C I/O 子集实现 C 风格流输入/输出操作。 <cstdio> 头文件提供通用文件支持并提供有窄和多字节字符输入/输出能力的函数&#xff0c;而 <cwchar>头文件提供有宽字符输入/输出能力的函数。 无格式输入/输出 从 stdin 读取字符 std::getchar int getch…

零基础Linux_3(基本指令_下)目录/文件的复制移动查看打包+其它指令

此篇接着上篇&#xff0c;所以目录也接着上篇了&#xff0c;上篇链接&#xff1a;零基础Linux_2(基本指令_上)目录/文件的显示跳转创建删除_GR_C的博客-CSDN博客 目录 6. 复制文件或目录 cp 源文件 目标文件(拷贝源文件到目标文件) cp -r 源目录 目标目录(拷贝源目录到目标…

苹果笔推荐购买吗?苹果ipad触控笔推荐

电容笔什么牌子好用&#xff1f;最近看到很多人在问这个问题。也是&#xff0c;现在的电容笔品牌太多了&#xff0c;选的人眼花缭乱的&#xff0c;不了解电容笔的人都不知道应该怎么选。下面&#xff0c;根据我多年使用电容笔的经验&#xff0c;来给大家推荐四款性价比高的电容…

LINUX 用户和组操作

目录 一、用户和组的分类 1、用户分类 2、组的分类 3、用户和组的配置文件 二、用户管理 1、添加用户 2、修改用户信息 3、修改用户密码 4、用户间切换 5、删除用户账号 6、sudo命令提高普通用户权限 三、用户组管理 1、创建用户组 2、修改用户组的属性 3、添加…

OpenCV基础(二):绘制直线、绘制几何图形、绘制文字、创建窗口

前言 在Android音视频开发中&#xff0c;网上知识点过于零碎&#xff0c;自学起来难度非常大&#xff0c;不过音视频大牛Jhuster提出了《Android 音视频从入门到提高 - 任务列表》&#xff0c;结合我自己的工作学习经历&#xff0c;我准备写一个音视频系列blog。本文是音视频系…

折半查找(二分查找)

1.算法思想 折半查找&#xff0c;又称“二分查找”&#xff0c;仅适用于有序的顺序表。 1.适用范围 顺序表拥有随机访问的特性&#xff0c;链表没有。 图解&#xff1a;&#xff08;low代表左区间边界&#xff0c;high代表右区间边界&#xff0c;mid代表中间元素&#xff0…

Nginx安装与常见命令

一、Nginx简介 官方文档&#xff1a;https://www.nginx.com/ Nginx中文文档&#xff1a;https://nginx.cn/doc/index.html Nginx由俄罗斯人&#xff08;Igor Sysoev&#xff09;编写的轻量级Web服务器&#xff0c;它的发音为 [ˈendʒɪnks] 。 Nginx 不仅是一款高性能的 HTTP服…

基于YOLOv8模型的烟头目标检测系统(PyTorch+Pyside6+YOLOv8模型)

摘要&#xff1a;基于YOLOv8模型的烟头目标检测系统可用于日常生活中检测与定位车辆目标&#xff0c;利用深度学习算法可实现图片、视频、摄像头等方式的目标检测&#xff0c;另外本系统还支持图片、视频等格式的结果可视化与结果导出。本系统采用YOLOv8目标检测算法训练数据集…

字符串的创建及常用方法大全

字符串 1.索引思维 值类型:不会改变原来的数 var arr 100//值类型function fn(a) {a 200}fn(arr)console.log(arr);//100引用类型:会改变原来的数组 // var arr [11, 22, 33, 44]// var arr1 arr;//arr1引用了arr的地址// arr1[0] "AA"// console.log(arr1);//…

js中运算规则

法&#xff1a; 有对象&#xff0c;对象是根据object.valueof().toString()返回的值 //toString的对象 var obj2 {toString:function(){return a} } console.log(2obj2) //输出结果2a//常规对象 var obj1 {a:1,b:2 } console.log(2obj1)&#xff1b; //输出结果 2[object…