vineet2004 has asked for the wisdom of the Perl Monks concerning the following question:

hello monks here's my problem...... i have a file ..... run.sh which when run creates a log file. the name of the log file contains its PID number(which of course changes each time i run run.sh.) say for example the PID number is 5434...then the logfile name would be hull-5434_static.log . my purpose is to be able to do the pattern matching...in other words the text processing of this log file. i have written a perl code for the text processing part ...and it works fine. the problem is that my code works only when i know the logfile's name. i am not able to dynamically get the name of the logfile each time run.sh runs. so what i want to be able to do is: 1....get the PID number of the log file as soon as run.sh runs. 2....get the logfile name using th PID number. 3....and finally execute my perl code at the prompt
bash# perl tp.pl hull-PID_static.log
i dont know if this problem involves shell scripting or perl ........please help me guys vineet

Replies are listed 'Best First'.
Re: scripting probs
by Fletch (Bishop) on Dec 27, 2006 at 12:25 UTC

    One simple solution would be to have the run.sh symlink a fixed name (say hull-last_static.log) to the output from its last run (perhaps when it finishes as its final action). Then your code would just use readlink to obtain the last run's logfile name, or simply open the symlink directly.

    Alternately you could use opendir/readdir and sort the results by the modification time (obtained with stat to find the newest file.

Re: scripting probs
by jesuashok (Curate) on Dec 27, 2006 at 07:48 UTC
    hi vineet2004

    #Shell script 1:- #!/usr/bin/ksh while true do ksh run.ksh sleep 5 done
    #Shell script 2:- #!/usr/bin/ksh echo "Hello World" >> $$.log echo "Hello World1" >> $$.log
    In the above scripts , run.ksh is called by another script. the run.ksh creats the log file with the PID. and you want this log file to be processed by the perl script.
    I assume that your log file will be created in some directory. For example :-
    If your directory is /tmp/log_dir, then
    perl parser.log `ls -rt /tmp/log_dir | tail -1`
    Once the log file is processed by your perl code then move the log file safely to the backup directory if you want or remove the log file, if you don't want the log file in future. Please customise the steps as per your requirement.