Reputation: 4085
Here's my high level problem.
I want to read a number max_age_in_secs
from a config file. This number represents the number of seconds we consider a file is "old". After we read this config, we want to search all files in a directory data
and delete all files modified before max_age_in_secs
seconds ago.
This has to be done as a shell script, more specifically korn shell.
What I'm doing right now is to touch a dummy file to be modified max_age_in_secs
ago, then use find
and its ! -newer
option to search for files older than that.
I'm having a problem getting a timestamp to pass to touch
. The date
command on my unix box doesn't have the option --date
so I can't use that.
I'm looking at perl -e "blah_blah_blah"
but I need to pass the max_age_in_secs
variable to the perl command. The only way I know to do that is creating a new .pl
file and pass arguments to that file. But ideally I would like to have this functionality in one korn script file. A workaround may be constructing a perl_command
variable, and execute its content, but I think that's not secure and vulnerable to injections.
But I'm relatively new to shell scripting and perl, so any help is appreciated.
Upvotes: 0
Views: 403
Reputation: 118595
There is more than one way to pass an environment variable from a shell to a one-line perl command:
perl -e "print $VAR"
so in this case $VAR
is coming from the shell. This technique has a lot of shortcomings, namely that with an `-e "double quoted expression" it is a pain to use other Perl variables or use a lot of other useful Perl constructions that might be misconstrued by the shell.
@ARGV
perl -e 'print "VAR is $ARGV[0]"' $VAR
This will do if you don't need to use @ARGV
(which are implicitly used if you use the -p
or -n
switches, or if your program uses the default <>
operator).
%ENV
hash.perl -e 'print "VAR is $ENV{VAR}"'
This mechanism doesn't interfere with @ARGV
Upvotes: 2
Reputation: 274532
Instead of seconds use minutes. Recent versions of find
have the mmin
option. For example, the following commands delete files older than a minute.
find /path/ -type f -mmin +1 -exec rm {} \;
Upvotes: 0
Reputation:
perl -e '... $ENV{max_age_in_secs}...'
as long as you do
export max_age_in_secs
in bash/sh/ksh
Upvotes: 1