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.
There is more than one way to pass an environment variable from a shell to a one-line perl command:
so in this case
$VARis 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.@ARGVThis will do if you don’t need to use
@ARGV(which are implicitly used if you use the-por-nswitches, or if your program uses the default<>operator).%ENVhash.perl -e 'print "VAR is $ENV{VAR}"'This mechanism doesn’t interfere with
@ARGV