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


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

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

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

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

Cenos7安装小火车程序动画

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

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

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

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

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

运维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) ,[email protected]
#    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) ,[email protected]
#    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": 权限不够
 


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

猜你喜欢

转载自blog.csdn.net/u014635374/article/details/132817952