Help with loop/counter script

bashscript

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.

Best Answer

  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>