1849253 Members
4215 Online
104042 Solutions
New Discussion

scripting

 
Raj Mithal_1
Advisor

scripting

I am battling to compare dates in a script. This is the output of lpstat -o. I want to delete anything thats older then 24hrs. How do I compare this date with sysdate.

apmsrep-5492 dklerkc priority 0 May 18 11:41
apmsrep-7963 dklerkc priority 0 May 18 12:26
phbclab-2715 potasr priority 0 May 23 15:05
pharec-692 potasr priority 0 Jun 24 10:39
pharec-693 potasr priority 0 May 25 09:23
rbstat2-6414 henriqt priority 0 May 25 11:04
phbclab-1613 potasr priority 0 May 27 07:42
phbclab-7175 potasr priority 0 May 27 12:47
rbphlab-160 dtoitf1 priority 0 Jun 24 11:17
rbphlab-206 dtoitf1 priority 0 May 30 09:32

5 REPLIES 5
Geoff Wild
Honored Contributor

Re: scripting

Hers a script I use - to help you get started:

# cat /usr/local/bin/lpqpurge
#!/bin/sh
#
# script to cancel jobs older then the current month
# run say on the 21st of each month
# gwild

# get the current month

M=`date +%b`

# cancel any jobs older then current month
# lpstat -o returns the following fields
# GB01-7606 ipradm priority 3 Apr 22 04:05 on GB01
# so, compare field 5 to the current month

for JOB in `lpstat -o |grep -v bytes|awk '$5ne"$M"{print $1}'`
do
cancel $JOB
done

# could have been done on 1 line like so:
# lpstat -o |grep -v bytes|awk '$5ne"`date +%b`"{print $1}'|xargs cancel
# but if there are no jobs older then current month - then cancel outputs error



Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Raj D.
Honored Contributor

Re: scripting

Hi Raj Mithal ,

Though its not very clear to me , what exactly are you looking for , can you please explain little bit.

As from the output its showing the dates all are older than 24hrs.

Raj.
" If u think u can , If u think u cannot , - You are always Right . "
RAC_1
Honored Contributor

Re: scripting

You can do some scripting as advised. The way I approach is as follows. Any print request , it goes to /var/spool/lp/request directory.

You can just run find command to know what is older as defined by you.

find /var/spool/lp/request -type f -mtime +2
Will list files older than 2 days. Then you can combine this into a script to delete such print reqests.
There is no substitute to HARDWORK
Pete Randall
Outstanding Contributor

Re: scripting

Or you can let the find command do the removal for you:

find /target -type f -mtime +1 -exec rm {} \;

See man find.


Pete

Pete
Raj D.
Honored Contributor

Re: scripting

Hi Raj ,
You can get the current date with many format , and need to compare with date in the lpstat -o output , and the job can be done.

$ date +%d%m will give in numeric format.
i.e 0209
$ date +%b%d
Sep02

$ date | awk '{print $2 $3}'
Sep2

Also the find command can be useful inside the script,

Cheers,
Raj.

" If u think u can , If u think u cannot , - You are always Right . "