Linux 如何在脚本运行时每X次删除一个文件-从脚本内部管理日志文件?
我通常只是将其作为cron作业或脚本进行调度,但是,我只希望在50次之后删除日志文件(每次脚本运行时都会不断地将其附加到日志文件中) 脚本内部需要: 问题是,由于脚本运行不一致,因此必须在脚本本身中实现它。请注意:出于各种原因,我需要在脚本中包含此内容 我所尝试的: 我在考虑将一个变量设置为递增,将其输出到一个文件,然后让脚本每次读取该文件。然后,如果该值大于X,则删除该文件。这部分代码将是Linux 如何在脚本运行时每X次删除一个文件-从脚本内部管理日志文件?,linux,bash,awk,sed,grep,Linux,Bash,Awk,Sed,Grep,我通常只是将其作为cron作业或脚本进行调度,但是,我只希望在50次之后删除日志文件(每次脚本运行时都会不断地将其附加到日志文件中) 脚本内部需要: 问题是,由于脚本运行不一致,因此必须在脚本本身中实现它。请注意:出于各种原因,我需要在脚本中包含此内容 我所尝试的: 我在考虑将一个变量设置为递增,将其输出到一个文件,然后让脚本每次读取该文件。然后,如果该值大于X,则删除该文件。这部分代码将是grep或awk语句 有谁知道一个更简单更好的方法吗?非常感谢您的积极投入 您可以使用xattr将任意元数
grep
或awk
语句
有谁知道一个更简单更好的方法吗?非常感谢您的积极投入 您可以使用
xattr
将任意元数据与文件关联,如下所示:
touch a.txt # Create file
xattr -w runs 1 a.txt # Set run count to 1
xattr -p runs a.txt # Print run count
1
xattr -w runs 49 a.txt # Change value
xattr -l a.txt # List all attributes
runs: 49
$ cat program.awk
BEGIN {
this=5 # the counter variable
}
/this=[0-9]+/ { # if counter (this=5) matches
if(this==0) # if counter is down to 0...
; # ... do what you need to do
split($0,a,"=") # split "this=5" by the "="
sub(/=[0-9]+$/,"=" (a[2]==0?5:a[2]-1)) # decrement it or if 0 set to 5 again
}
1 # print
其美妙之处在于它不会出现在grep
中,也不会出现在使用普通工具查看文件时
请注意,并非所有文件系统(如Microsoft FAT)都支持
xattr
您可以使用xattr
将任意元数据与文件关联,如下所示:
touch a.txt # Create file
xattr -w runs 1 a.txt # Set run count to 1
xattr -p runs a.txt # Print run count
1
xattr -w runs 49 a.txt # Change value
xattr -l a.txt # List all attributes
runs: 49
$ cat program.awk
BEGIN {
this=5 # the counter variable
}
/this=[0-9]+/ { # if counter (this=5) matches
if(this==0) # if counter is down to 0...
; # ... do what you need to do
split($0,a,"=") # split "this=5" by the "="
sub(/=[0-9]+$/,"=" (a[2]==0?5:a[2]-1)) # decrement it or if 0 set to 5 again
}
1 # print
其美妙之处在于它不会出现在grep
中,也不会出现在使用普通工具查看文件时
请注意,并非所有的文件系统(例如Microsoft FAT)都支持
xattr
,因为Gnu awk v.4.1版提供了就地编辑
(请参阅)因此,您可以将计数器存储到awk脚本变量中,并使用awk脚本编辑自身并按如下方式递减计数器变量:
touch a.txt # Create file
xattr -w runs 1 a.txt # Set run count to 1
xattr -p runs a.txt # Print run count
1
xattr -w runs 49 a.txt # Change value
xattr -l a.txt # List all attributes
runs: 49
$ cat program.awk
BEGIN {
this=5 # the counter variable
}
/this=[0-9]+/ { # if counter (this=5) matches
if(this==0) # if counter is down to 0...
; # ... do what you need to do
split($0,a,"=") # split "this=5" by the "="
sub(/=[0-9]+$/,"=" (a[2]==0?5:a[2]-1)) # decrement it or if 0 set to 5 again
}
1 # print
运行它:
$ awk -i inplace -f program.awk program.awk
$ head -3 program.awk
BEGIN {
this=4 # the counter variable
}
基本上,您运行
program.awk
,它在program.awk
中更改一条记录,一旦计数器点击0,则执行if
自Gnu awk v.4.1以来,已提供就地编辑
(请参阅)因此,您可以将计数器存储到awk脚本变量中,并使用awk脚本编辑自身并按如下方式递减计数器变量:
touch a.txt # Create file
xattr -w runs 1 a.txt # Set run count to 1
xattr -p runs a.txt # Print run count
1
xattr -w runs 49 a.txt # Change value
xattr -l a.txt # List all attributes
runs: 49
$ cat program.awk
BEGIN {
this=5 # the counter variable
}
/this=[0-9]+/ { # if counter (this=5) matches
if(this==0) # if counter is down to 0...
; # ... do what you need to do
split($0,a,"=") # split "this=5" by the "="
sub(/=[0-9]+$/,"=" (a[2]==0?5:a[2]-1)) # decrement it or if 0 set to 5 again
}
1 # print
运行它:
$ awk -i inplace -f program.awk program.awk
$ head -3 program.awk
BEGIN {
this=4 # the counter variable
}
基本上,您运行
program.awk
,它在program.awk
中更改一条记录,一旦计数器点击0,则执行if
如前所述,我的主要要求是在脚本内部或行内执行此操作。正如@EvansWinner提到的,我可以通过使用文件大小或年龄等属性来管理文件。然而,我使用了更简单的方法,使用了行数
sed -i '100000,$ d' file.txt
因此,我不必担心它运行了多少次。希望对于试图在脚本中每隔x次删除一个文件的人,这将帮助您查看文件属性,以及如何使用大小、年龄或我使用的行数来管理它们。这些解决方案比创建其他系统所需的程序或包更具可移植性。如前所述,我的主要要求是在脚本内部或在线执行此操作。正如@EvansWinner提到的,我可以通过使用文件大小或年龄等属性来管理文件。然而,我使用了更简单的方法,使用了行数
sed -i '100000,$ d' file.txt
因此,我不必担心它运行了多少次。希望对于试图在脚本中每隔x次删除一个文件的人,这将帮助您查看文件属性,以及如何使用大小、年龄或我使用的行数来管理它们。这些解决方案比创建其他系统上所需的程序或包更具可移植性。在下面的代码中使用
sed
,每次运行脚本时,脚本中的x
将增加1
,最多增加50
,然后将x
设置回1
。您可以将该命令设置为处理if
语句的else
分支中的日志文件,以及要在每个分支中运行的任何其他代码
#/bin/bash
x=1
y=$((x+1))
z=1
若[$x-lt 50];然后
#做点什么。。。
sed-i-e“s/x=$x/x=$y/”“$0”
其他的
#做点什么。。。
#删除日志文件。。。
sed-i-e“s/x=$x/x=$z/”“$0”
fi
在这里,我运行脚本以显示
x
在运行50次后递增并重置回1
:
$cat测试脚本
#!/bin/bash
x=1
y=$((x+1))
z=1
若[$x-lt 50];然后
#做点什么。。。
sed-i-e“s/x=$x/x=$y/”“$0”
其他的
#做点什么。。。
#删除日志文件。。。
sed-i-e“s/x=$x/x=$z/”“$0”
fi
$./testscript
$cat测试脚本
#!/bin/bash
x=2
y=$((x+1))
z=1
若[$x-lt 50];然后
#做点什么。。。
sed-i-e“s/x=$x/x=$y/”“$0”
其他的
#做点什么。。。
#删除日志文件。。。
sed-i-e“s/x=$x/x=$z/”“$0”
fi
$
如您所见,脚本中的x=1
已变成x=2
我现在手动将x=2
设置为x=50
,并保存脚本以显示它重置为x=1
$cat测试脚本
#!/bin/bash
x=50
y=$((x+1))
z=1
若[$x-lt 50];然后
#做点什么。。。
sed-i-e“s/x=$x/x=$y/”“$0”
其他的
#做点什么。。。
#删除日志文件。。。
sed-i-e“s/x=$x/x=$z/”“$0”
fi
$./testscript
$cat测试脚本
#!/bin/bash
x=1
y=$((x+1))
z=1
若[$x-lt 50];然后
#做点什么。。。
sed-i-e“s/x=$x/x=$y/”“$0”
其他的
#做点什么。。。
#删除日志文件。。。
sed-i-e“s/x=$x/x=$z/”“$0”
fi
$
在下面的代码中使用sed
,每次运行脚本时,脚本中的x
将增加1
,最多增加到50
,然后将x
设置回1
。您可以将该命令设置为处理if
语句的else
分支中的日志文件,以及要在每个分支中运行的任何其他代码
#/bin/bash
x=1
y=$((x+1))
z=1
若[$x-lt 50];然后
#做点什么。。。