Operating System - HP-UX
1833897 Members
2054 Online
110063 Solutions
New Discussion

using tail -f in a script

 
SOLVED
Go to solution

using tail -f in a script

Working in HP-UX 10.20. I eventually want to write a bourne shell script to handle the following problem, but for now I am just toying with it at the command line.

Here's what I am basically trying to do:

tail -f log_X | grep n > log_Y

I am doing a tail -f on log_X . Once it sees "n", I would like for it to grep it, then put it into log_Y. It ain't making it to log_Y.

I have been manually adding "n" to log_X, and the tail command is definitely seeing it, but it fails to pass it to log_Y.

Why? Is it because the command is trying to "complete" the tail -f before it executes the > (redirect) to log_Y??

Is there a better way to appraoch this?

TYIA

14 REPLIES 14
Clemens van Everdingen
Honored Contributor

Re: using tail -f in a script

Hi,

Use tail -100 log_X

C.
The computer is a great invention, there are as many mistakes as ever, but they are nobody's fault !
John Poff
Honored Contributor

Re: using tail -f in a script

Hi,

I'm not sure if tail -f will work like you want it to. If I were going to try to do it, I would use a Perl script and get the Tail module from CPAN. The Tail module looks like it will do what you are looking for, and inside the Perl script you can grab what you need and write it to another file. I'm sure one of the local Perl experts will pick up on this thread and show you how to really do it right.

Just one way to try it.

JP

Re: using tail -f in a script

JP:

That would be helpful...but, I don't know PERL.

TYIA
john korterman
Honored Contributor

Re: using tail -f in a script

Hi,
you could also try something like this:

tail -1f log_X | while read line
do
echo $line | grep n
if [ "$?" = "0" ]
then
# found, append
echo $line >>/log_Y

fi
done


regards,
John K.
it would be nice if you always got a second chance

Re: using tail -f in a script

Before I try the PERL suggestion, lemme throw this out there.

Lets suppose I do a ls -la /etc/myfile. Here's what I expect to see:

-r--r--r-- 1 root sys 17867 Oct 21 13:37 /etc/myfile.

I am interested in the file size of this file, which in this case is 17867.

I could do a ls -la /etc/myfile | awk '{print $5}' and it would echo 17867...right?

That's cool, but what if I want to do something other than printing $5. Let's say i wanna store it, then do something else based on it's value.

For example (and I realize that STORE is not a real option here):

ls -la /etc/myfile | awk '{STORE $5}'

if [ $5 = 0 ];
echo All is well
else
echo All is not well
fi


What is the magic word here? I know it is not STORE. All I can find on using awk is the "print" option.
I don't wanna print $5, I wanna store it then use it in an IF statement.

Ideas?

TYIA
John Poff
Honored Contributor

Re: using tail -f in a script

Hi,

You can store the results of your ls command in a variable, and test that variable in an if statement. Try something like this:

FILESIZE=$ls -la /etc/myfile | awk '{STORE $5}')

if (( $FILESIZE -eq = 17867 ));
echo All is well
else
echo All is not well
fi




JP


Christian Gebhardt
Honored Contributor

Re: using tail -f in a script

Hi
you cam do the if-statement in awk:
ls -la /etc/myfile | awk '{if ( $5 == 0) print "ok"; else print "notok"}'
Chris
Hamdy Al-Sebaey
Regular Advisor

Re: using tail -f in a script

Hi,

I'm not sure if tail -f will work fine or not,but usually I use for example tail -50 or 150 logfile.
regards,
Hamdy
Thanks for sharing knowledge

Re: using tail -f in a script

John K:

I tried your suggestion. Same thing happened...the tail catches the "n", but it is not passed to log_Y.

?????



TYIA
Hamdy Al-Sebaey
Regular Advisor

Re: using tail -f in a script

Hi Robert,

What about,for example
tail -50 /var/adm/syslog/syslog.log > /tmp/robert.
regards,
Hamdy
Thanks for sharing knowledge
John Poff
Honored Contributor

Re: using tail -f in a script

Hi,

My example is messed up. I can't write shell scripts until I get enough caffeine into my system!

Christian's awk example will do the job.

JP
john korterman
Honored Contributor
Solution

Re: using tail -f in a script

Hi again,
sounds strange. I have just tried this:

1) created a logfile
# touch /tmp/dunavent

2) made the script dunavent.sh with this content:
#!/usr/bin/sh
LOGFILE=/tmp/dunavent
tail -1f $LOGFILE | while read line
do
echo $line | grep e
if [ "$?" = "0" ]
then
# found
echo $line >>./log_Y
fi
done

3) executed dunavent.sh in the background:
# dunavent.sh &

4) added lines to /tmp/dunavent
# echo "ee and e" >>/tmp/dunavent

And all lines containing "e" are added to log_Y
(!?)
How did you do it?

regards,
John K.

it would be nice if you always got a second chance
Jean-Louis Phelix
Honored Contributor

Re: using tail -f in a script

Hi,

Here is a shell which does it without a daemon :

#!/usr/bin/sh
tail -f log_X |&
NPID=$!
while :
do
read -p VAR
echo $VAR | grep n > log || continue
kill $!
break
done
cat log

Regards,

Jean-Louis.
It works for me (© Bill McNAMARA ...)

Re: using tail -f in a script

John K:

Your script did the trick for me!!! Thanks!!

-cd