1827752 Members
3332 Online
109969 Solutions
New Discussion

Re: ksh script help

 
SOLVED
Go to solution
brian_31
Super Advisor

ksh script help

Team:

I need to get this done in ksh script. I have the
following output from lpstat -o (we are trying to
capture the jobs which are in the print queue for 7
days or more). lpstat -o output is attached. Please
help.

Thanks

Brian.
15 REPLIES 15
brian_31
Super Advisor

Re: ksh script help

Hi again:

We are trying to capture the jobs which are 7 days or more in the queue and email them to admins.

Thanks

Brian
RAC_1
Honored Contributor

Re: ksh script help


#!/usr/bin/ksh

7day_back=`perl -e '$sevenday = localtime(time-24*60*60),print "$sevenday\n"'|awk '{print $2, $3}'`

lpstat -o | grep "${7day_bak}"

Anil
There is no substitute to HARDWORK
TwoProc
Honored Contributor

Re: ksh script help

This should work... but I can't test it much b/c my queues are pretty clean right now...

#!/bin/ksh
export HNAME=`/bin/hostname`
cd /var/spool/lp/request
for i in `find . -type f -name "cA*${HNAME}" -mtime +7`
do
printer=`echo $i | cut -f 2 -d"/"`
job=`echo $i | cut -f 3 -d"/"| sed -e "s/^cA//" -e "s/$HNAME//"`
echo Print Job $job on printer $printer is older than 7 days
done | mailx -s "OLD PRINT JOBS" admin@urcompany.com
We are the people our parents warned us about --Jimmy Buffett
brian_31
Super Advisor

Re: ksh script help

Thanks. I also need to email the results to admins. PLUS i need the script to work on the output that i have given. Please help

Thanks

Brian
TwoProc
Honored Contributor
Solution

Re: ksh script help

the mailx command at the end of the script I sent will send out the mail...
The script I wrote doesn't use your output -
it uses the fact that those files will reside in the output queue directories to create a simple find command to do the work.
But, it should be a simple thing to get the one Anil posted to work just pipe it to mailx command like I have at the end of the command I posted above.
We are the people our parents warned us about --Jimmy Buffett
brian_31
Super Advisor

Re: ksh script help

Hi:

There was a typo in the ksh script. I changed that but i still get the broken pipe error. Wonder what the issue would be?

Thanks

Brian.
Hoang Chi Cong_1
Honored Contributor

Re: ksh script help

Think that you should remove it!
Looking for a special chance.......
brian_31
Super Advisor

Re: ksh script help

hi:

This is what i get when i execute the script

$./print.sh
./print.sh[4]: 7day_back=Mar: not found
./print.sh[6]: "${7day_back}": bad substitution
$Broken Pipe

Please help
harry d brown jr
Honored Contributor

Re: ksh script help

you can NOT have numbers as the first chracter in a variable name!

SevenDaysBack=`perl -e '$sevenday = localtime(time-(7*24*60*60));print "$sevenday\n"'|awk '{print $2, $3}'`

and of course 7 days back is time - (7 * 24 * 60 *60)

live free or die
harry d brown jr
Live Free or Die
Henk Pilon
Trusted Contributor

Re: ksh script help

Just don't start the name of a variable with a digit. ksh expects that to be a standard variable passed to the script at the commandline. ($0 - $9)
Change 7days_back to days_back or sevendays_back

Good luck
brian_31
Super Advisor

Re: ksh script help

anil/all:

I am getting null string for $sevenday $2 and $3 these are also null. please help.

Thanks

brian
harry d brown jr
Honored Contributor

Re: ksh script help

What do you get when you run this:

perl -e '$sevenday = localtime(time-(7*24*60*60));print "$sevenday\n"'|awk '{print $2, $3}'

or this:

perl -e '$sevenday = localtime(time-(7*24*60*60));print "$sevenday\n"'

live free or die
harry d brown jr
Live Free or Die
Hein van den Heuvel
Honored Contributor

Re: ksh script help

Hmmm,

that $sevendays thing works fine for me:

> ksh
$ SevenDaysBack=`perl -e '$sevenday = localtime(time-(7*24*60*60));print "$seven
day\n"'|awk '{print $2, $3}'`

$ echo $SevenDaysBack
Mar 25


fwiw, Personally it rubs me the wrong way to follow perl with awk when a trivial perl solution is available. For example:

$ perl -e '$_ = localtime(time-(7*24*60*60)); @old=split; print "$old[1] $old[2]
\n"'
Mar 25
$

To carry that theme forward, why not compare the dates in perl? Now the 'Mar 31' style date is hard to compare. So first convert it to seconds. Then compare with current time in seconds minus cutoff time. I don't know how to have perl do month-string to month-number nicely (Language independend, so I cheated and hardcoded a month table:

Start of a solution:

---- find_old.p ----
use Time::Local;
$year = (localtime)[5];
while (<>){
if (/(\w+)\s+(\d+)\s+(\d+):(\d+)$/) {
$mon = index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)/3;
$t = timelocal(0,$4,$3,$2,$mon,$year);
print if ($t < time - (7*24*60*60))
}
}

---- sample output for slightly editted input sample ---

perl find_old.p lpstat.txt
brflora-295 aflorad1@essdbdu31 4330 Mar 21 19:30
brflora-298 aflorad1@essdbdu31 5444 Feb 2 19:30


It should be straight forwards to just print names, numbers, or email from that perl script.

Good luck,
Hein.
Rory R Hammond
Trusted Contributor

Re: ksh script help

Brian,

I know that you are focused on the lpstat command.
I approached the problem differently
cd /var/spool/lp/request
find . -type file -mtime -7 -print
and then process that list.

good luck
Rory
There are a 100 ways to do things and 97 of them are right
brian_31
Super Advisor

Re: ksh script help

Thanks Harry, Hein. It works. What a Forum this is!!!! Unbeleivable.

Thanks a ton

Brian