Help with loop/counter script 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 "&lt;result&gt;"
for ((counter=0; counter &lt; "$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 "&lt;/result&gt;"


The output:

&lt;result&gt;
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

&lt;/result&gt;


Thanks.
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}'

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 "&lt;result&gt;"
 for ((counter=0; counter &lt; "$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 "&lt;/result&gt;"


The output (for my installed printers):

&lt;result&gt;
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

&lt;/result&gt;