1758660 Members
2385 Online
108874 Solutions
New Discussion юеВ

Help with script

 
SOLVED
Go to solution
Nick D'Angelo
Super Advisor

Help with script

Experts,

I am trying to write a script that will run every 1/2 hour through cron that will check to see if someone submits a print job to a specific printer. If it finds a print job, then it will send me an email, otherwise, it would just go away and sleep until the next time.

This is what I have so far and the part I have a problem with is the if command. If it finds a print job going to , then send email.

#!/usr/bin/ksh
#

RETVAL=0
PATH=$PATH:/usr/sam/lbin:/usr/sam/bin:/usr/bin:/usr/sbin:/sbin;export PATH
#
echo "Subject: Check printer user"`date` >>/tmp/echkmail.$$
echo "This routine is about to check the EW Cheque printer for : " >>/tmp/echkmail.$$
echo "Users that are not authorized to use it.!! " >>/tmp/echkmail.$$
echo " " >> /tmp/echkmail.$$
lpstat -o|grep echeck1 >> /tmp/echeckyes
grep echeck1 /tmp/echeckyes
if [ $RETVAL -ne 0 ]
then
sendmail nickd < /tmp/echeckyes
else
echo "No echeck1 print files found" >> /tmp/echkmail.$$
fi
#
# mail out results
#
sendmail nickd < /tmp/echkmail.$$
sendmail nickd < /tmp/echeckyes.$$
#rm /tmp/echkmail*

Suggestions are always appreciated,

ND
Always learning
4 REPLIES 4
Stefan Schulz
Honored Contributor

Re: Help with script

Hi Nick,

i would replace the part:

lpstat -o|grep echeck1 >> /tmp/echeckyes
grep echeck1 /tmp/echeckyes
if [ $RETVAL -ne 0 ]

with:

if [ $(lpstat -t | grep -c echeck1-) ]

This will do the following. In the lpstat if there is a job for the printer echeck1 it will have the jobid echeck1-12345 or so. With the grep command you look for those jobid's and count them (-c). If there is no job the result will be 0 or false. If there is one or more job in the queue the result will be 1 or higher which means true.

Hope this helps. Stefan
No Mouse found. System halted. Press Mousebutton to continue.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Help with script

Hi Nick:

The problem is that your not really testing the return value from the 'grep' of the 'lpstat' call. Try something like this:

...
lpstat -o|grep echeck1 > /dev/null
if [ $? -eq 1 ]
then
echo "nothing found"
else
echo "found a match"
fi

The idea of redirecting the output to /dev/null is simply that you don't need anything more than the true/false result which you test as above.

...JRF...

...JRF...
Laurent Paumier
Trusted Contributor

Re: Help with script

Or simply :

if lpstat -o | grep -q echeck1
then
echo "nothing found"
else
echo "found a match"
fi
Fred Martin_1
Valued Contributor

Re: Help with script

Can't you just review the lp log? On my system I can do this:

grep printername /var/adm/lp/log

It shows all past print jobs, with usernames, dates and times, like this:

xplr-2229 xdk1 xplr May 31 09:28

Fred

fmartin@applicatorssales.com