Operating System - HP-UX
1771005 Members
2748 Online
109003 Solutions
New Discussion юеВ

Shell Script to report total print jobs for each que

 
Bill Hassell
Honored Contributor

Re: Shell Script to report total print jobs for each que

Sorry I did not see the question earlier. The cut -d \ -f .. is a way to specify a single space as a delimiter. Unfortunately, the forums code reduces multiple spaces to just one. So when you cut-n-paste the text, it will produce the error you see. The text should read:

cut -d \space space -f ...

so leave out the apostrophes and just add an extra space after the \ char. The \ escapes the special meaning of the first space and the missing second space separates -d char from the -f option. Another way is:

cut -d ' ' -f ...

which is probably more intuitive and translates cleanly:

#!/usr/bin/sh
# Count print requests by printer

export PATH=/usr/bin
ACTIVE=0
JOBS=0
for PRINTQ in $(echo /var/spool/lp/request/*)
do
QUEUENAME=$(basename $PRINTQ)
QTY=$(ls $PRINTQ/d* 2>/dev/null | wc -l | cut -d ' ' -f 1)
if [ $QTY -gt 0 ]
then
let ACTIVE=$ACTIVE+1
print "Printer $QUEUENAME = \c"
print "$QTY request\c"
[[ $QTY -ne 1 ]] && print "s\c"
let JOBS=$JOBS+$QTY
print ""
fi
done
print "\nTotal jobs=$JOBS, printers with a queue=$ACTIVE"

(I checked a cut-n-paste from the above and it works OK)


Bill Hassell, sysadmin
Tracy Force
Occasional Contributor

Re: Shell Script to report total print jobs for each que

can someone explain what this line is doing?.
[[ $QTY -ne 1 ]] && print "s\c"
thanks
Bill Hassell
Honored Contributor

Re: Shell Script to report total print jobs for each que

The [[ $QTY -ne 1 ]] && print "s\c" line is a quick way to improve spelling. Instead of always saying "1 requests or "2 requests", the letter s is appended only when the quantity of requests is not 1. The 2 square brackets are used as an alternate way to test a condition, and the double ampersands is a shorthand way to say: do this if true. If two vertical bars were used, it would signify: do this if false. Both constructs can be used on the same line. The "s\c" means print "s" but do not add a new line, just leave the cursor where it is.


Bill Hassell, sysadmin