Posted on 07-07-2016 12:13 AM
I need to create a report showing a list of computers, and a column with the installed printers on each of the listed Macs.
We know you can pull up a computer and check under Computer > Inventory > Printers to get details on installed printers (Name, Model, URI, and Location).
I'm trying to come up with an Extension Attribute that lists installed printers, else return <result>NoPrintersInstalle</result>
if no printers are installed.
Here's what I have:
#!/bin/sh
RESULT=`lpstat -p | awk '{print $2}' | sed '/^$/d'`
EMPTY="lpstat: No destinations added."
if [[ ${RESULT} != $EMPTY ]]; then
echo "<result>$RESULT</result>"
else
echo "<result>NoPrintersInstalled</result>"
fi
And this is what I'm getting:
$ /tmp/test.sh
lpstat: No destinations added.
<result></result>
And this is how I feel as I wrap up to go home. :)
Any idea what I'm missing?
TIA,
Don
Solved! Go to Solution.
Posted on 11-02-2016 10:43 AM
I had not really tried the final script, but @jhuls is correct. I have 3 printers installed/set up on my Mac. Running the script only shows a result of one, specifically the first in the list, not all 3.
I didn't really dig too much into why its not showing the full results. I suspect its because of piping the results from the first lpstat -p command into another to create the the EMPTY variable.
Here's a revised version that works. It drops the items into an array, assuming anything gets returned from the command, then checks to see if the array is empty. If not, uses printf to print the array with each printer name on its own line, or prints the "NoPrintersInstalled" result.
#!/bin/sh
RESULT=($(lpstat -p 2>/dev/null | awk '{print $2}' | sed '/^$/d'))
if [[ -z "${RESULT[@]}" ]]; then
echo "<result>NoPrintersInstalled</result>"
else
echo "<result>$(printf '%s
' "${RESULT[@]}")</result>"
fi
Posted on 07-07-2016 07:32 AM
I assume you're only getting that when there are no printers installed, yes? My guess on the problem is that both the $EMPTY and $RESULT variables are not double quoted in the if statement, so its not actually getting matched and thus printing out the "$RESULT" variable. Try it like:
#!/bin/sh
RESULT=`lpstat -p | awk '{print $2}' | sed '/^$/d'`
EMPTY="lpstat: No destinations added."
if [[ "${RESULT}" != "$EMPTY" ]]; then
echo "<result>$RESULT</result>"
else
echo "<result>NoPrintersInstalled</result>"
fi
Posted on 07-07-2016 10:58 AM
When a command runs, it outputs data in multiple ways - the obvious being Standard Output and Error message. So I say 'cat /tmp/toddwashere' it will Standard Output the data to the terminal. If the file does not exist, it'll output an Error message. "No destinations added" comes as an error message, not Standard Out, which means it is not put in to the RESULT variable.
You should redirect Error message to standard output, so that if it says "No destinations added", that'll be saved in your Result variable. Do that with '2>&1'. Right now, the "No Destinations" text is going to the Standard Error output which is terminal and the RESULT variable stays null.
#!/bin/sh
RESULT=`lpstat -p 2>&1`
EMPTY="lpstat: No destinations added."
if [[ ${RESULT} != $EMPTY ]]; then
RESULTSSMALL=$(echo $RESULT | awk '{print $2}' | sed '/^$/d')
echo "<result>$RESULTSSMALL</result>"
else
echo "<result>NoPrintersInstalled</result>"
fi
EDIT: another option is to redirect ERROR message to /dev/null to silence them. Then if RESULT is empty, you know no printers are installed. -z means if variable is null (no data in it).
#!/bin/sh
RESULT=`lpstat -p 2>/dev/null` #2 is STD-ERR, redirect to NULL to silence.
if [ -z "$RESULT" ]; then #if RESULT is empty
echo "<result>NoPrintersInstalled</result>"
else
SlimOut=$(echo $RESULT | awk '{print $2}' | sed '/^$/d')
echo "<result>$SlimOut</result>"
fi
Posted on 11-01-2016 03:07 PM
@mm2270 and @thoule my apologies for the VERY late response. We ended up going with a mix/match of your solutions, works great, thanks!
#!/bin/sh
RESULT=$( lpstat -p 2>/dev/null )
EMPTY=$( echo $RESULT | awk '{print $2}' | sed '/^$/d' )
if [ -z "$RESULT" ]; then
echo "<result>NoPrintersInstalled</result>"
else
echo "<result>$EMPTY</result>"
fi
Posted on 11-02-2016 09:56 AM
@donmontalvo I just tried this script on my own system locally(Sierra) with 5 printers installed and it only lists the first printer in the list. It should be a list, right?
Posted on 11-02-2016 10:43 AM
I had not really tried the final script, but @jhuls is correct. I have 3 printers installed/set up on my Mac. Running the script only shows a result of one, specifically the first in the list, not all 3.
I didn't really dig too much into why its not showing the full results. I suspect its because of piping the results from the first lpstat -p command into another to create the the EMPTY variable.
Here's a revised version that works. It drops the items into an array, assuming anything gets returned from the command, then checks to see if the array is empty. If not, uses printf to print the array with each printer name on its own line, or prints the "NoPrintersInstalled" result.
#!/bin/sh
RESULT=($(lpstat -p 2>/dev/null | awk '{print $2}' | sed '/^$/d'))
if [[ -z "${RESULT[@]}" ]]; then
echo "<result>NoPrintersInstalled</result>"
else
echo "<result>$(printf '%s
' "${RESULT[@]}")</result>"
fi