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
@donmontalvo
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
@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
@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?
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