1

I'm trying to create a simple loop/counter script to display information about installed printers. Main the Name, PPD, PPD Version, and URI. I have two problems... 1 - the name is not coming up; 2 - the second printer PPD is incorrect.

Here what I have:

#!/bin/bash
name=($(/usr/sbin/system_profiler SPPrintersDataType | /usr/bin/awk -F ":" '/Name/{$1="";print $0}' | /usr/bin/sed 's/\ /-,--/g'))
ppd=($(/usr/sbin/system_profiler SPPrintersDataType | /usr/bin/awk -F ":" '/PPD/{$1="";print $0}' | /usr/bin/sed 's/\ /-,--/g'))
ppdversion=($(/usr/sbin/system_profiler SPPrintersDataType | /usr/bin/awk -F ":" '/PPD File Version/{$1="";print $0}' | /usr/bin/sed 's/\ /-,--/g'))
uri=($(/usr/sbin/system_profiler SPPrintersDataType | /usr/bin/awk '/URI/{print $NF}'))
total="${#ppdversion[@]}"
/bin/echo -n "<result>"
for ((counter=0; counter < "$total"; counter++))
{
    echo "name: ${name[$counter]}" | /usr/bin/sed 's/-,--/\ /g;s/\ \ //g'
    echo "PPD: ${ppd[$counter]}" | /usr/bin/sed 's/-,--/\ /g;s/\ \ //g'
    echo "PPD Version: ${ppdversion[$counter]}" | /usr/bin/sed 's/-,--/\ /g;s/\ \ //g'
    echo "URI: ${uri[$counter]}"
    echo
}
echo "</result>"

The output:

<result>
name: 
PPD: Canon MX410 series
PPD Version: 16.10.0.0
URI: lpd://printer.local


name: 
PPD: 16.10.0.0
PPD Version: 16.10.0.0
URI: dnssd://Canon%20MX410%20series%20_1B181C000000._printer._tcp.local./auto

</result>

Thanks.

1
  1. The script is searching for "Name" - but no field entitled "Name" exists, so nothing is found. There is an alternate command lpstat that may be easier to parse the name from, and returns the same ordering:

    /usr/bin/lpstat -p | awk '{print $2}'
    
  2. Since PPD is also in PPD Version, this causes a conflict. Search instead for PPD:, like the following:

    ppd=($(/usr/sbin/system_profiler SPPrintersDataType \
               | /usr/bin/awk -F ":" '/PPD:/{$1="";print $0}' \
               | /usr/bin/sed 's/\ /-,--/g'))
    

Here's a complete script - it appears to do what you want:

 #!/bin/bash
 name=($(/usr/bin/lpstat -p | awk '{print $2}' | /usr/bin/sed 's/\ /-,--/g'))
 ppd=($(/usr/sbin/system_profiler SPPrintersDataType | /usr/bin/awk -F ":" '/PPD:/{$1="";print $0}' | /usr/bin/sed 's/\ /-,--/g'))
 ppdversion=($(/usr/sbin/system_profiler SPPrintersDataType | /usr/bin/awk -F ":" '/PPD File Version/{$1="";print $0}' | /usr/bin/sed 's/\ /-,--/g'))
 uri=($(/usr/sbin/system_profiler SPPrintersDataType | /usr/bin/awk '/URI/{print $NF}'))
 total="${#ppdversion[@]}"
 /bin/echo "<result>"
 for ((counter=0; counter < "$total"; counter++))
 {
     echo "name: ${name[$counter]}" | /usr/bin/sed 's/-,--/\ /g;s/\ \ //g'
     echo "PPD: ${ppd[$counter]}" | /usr/bin/sed 's/-,--/\ /g;s/\ \ //g'
     echo "PPD Version: ${ppdversion[$counter]}" | /usr/bin/sed 's/-,--/\ /g;s/\ \ //g'
     echo "URI: ${uri[$counter]}"
     echo
 }
 echo "</result>"

The output (for my installed printers):

<result>
name: Brother_MFC_9325CW
PPD: Brother MFC-9325CW CUPS
PPD Version: 4.5.2
URI: usb://Brother/MFC-9325CW?serial=000L2J998184

name: Canon_MX410
PPD: Canon MX410 series
PPD Version: 16.10.0.0
URI: dnssd://Canon%20MX410._printer._tcp.local./auto

</result>
  • Thanks! Any idea what the printer name field is? – Webster Master May 4 '18 at 4:02
  • @WebsterMaster Working on it... :) – Scot May 4 '18 at 4:03
  • This places a bet on lpstat and system_profiler using the same sort order for printers :-) – nohillside May 4 '18 at 11:42
  • The name actually is in the output, but difficult to parse. May be easier to run system_profiler -xml and use one of the tools in stackoverflow.com/questions/893585/… to parse the XML result. – nohillside May 4 '18 at 11:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .