Passing three variables to loop and error

I have many printer queues to be created in AIX 6.1. I can create printers with this line:

/usr/lib/lpd/pio/etc/piomkjetd mkpq_jetdirect -p 'hplj-4000' -D pcl -q 'emg1' -D ps -q 'emg1ps' -h 'emg1' -x '9100'  

But, I like to feed the printer queue data, called "printfeed", so that I can loop to avoid a lot of manual entries.

cat printfeed
emg1  emg1ps  emgj1
emg2  emg2ps  emgj2
..... snipped .....
zeba                  zeba
emr12               emr12
..... snipped .....

(note)  the second column can be empty

I want to pass the first column as $q1, the second column as $q2, and the third column as $q3.

for pqn in $(cat printfeed)
do
q1 = cat $pqn |awk '{print $1}'
q2 = cat $pqn |awk '{print $2}'
q3 = cat $pqn |awk '{print $3}'
/usr/lib/lpd/pio/etc/piomkjetd mkpq_jetdirect -p 'hplj-4000' -D pcl -q '$q1' -D ps -q '$q2' -h '$q3' -x '9100'  
done  

With this script, I am getting:

ksh[3]: q1:  not found.
ksh[4]: q2:  not found.
ksh[5]: q3:  not found.
Host name $q3 does not exist.  A valid host name is required!

Really appropriate your help!

replace:

q1 = cat $pqn |awk '{print $1}'

by

q1=$(awk '{print $1}' <<< $pqn)

Use double-quotes, not single-quotes.

Rather than running 6 useless short-lived external programs for each and every single line, you can run zero:

while read VAR1 VAR2 VAR3
do
        /usr/lib/lpd/pio/etc/piomkjetd mkpq_jetdirect -p 'hplj-4000' -D pcl -q "$VAR1" -D ps -q "$VAR2" -h "$VAR3" -x '9100'  
done < /path/to/inputfile

If the second value can be empty in your input file, use Corona688's proposal and adapt:

while read q1 q2 q3
  do [ "$q3" ] || { q3=$q2; q2=""; }
     /usr/lib/lpd/pio/etc/piomkjetd mkpq_jetdirect -p 'hplj-4000' -D pcl -q "$q1" -D ps -q "$q2" -h "$q3" -x '9100'   
  done <printfeed

RudiC & Corona688,

It works charming except one error:

0782-310 Usage:
        mkvirprt -A AttachmentType
        mkvirprt [ -A AttachmentType ] -d QueueDevice -n Device
         -q PrintQueue -s DataStream -t PrinterType [ -T ]
        Use 'smit mkvirprt' to use it interactively.

dspmsg: 1312-042 Invalid argument index in message.  May need
more arguments on the command line.

The script runs fine for the ones having three columns defined in the feed file

cat printfeed
emg1  emg1ps  emgj1
emg2  emg2ps  emgj2
..... snipped .....
zeba                  zeba
emr12               emr12
..... snipped .....
(note)  the second column can be empty

But, the ones with empty second column returns the error above.

I think I need to modify the feed file with delimiter (pipe -- |):

cat printfeed
emg1 | emg1ps | emgj1
emg2 | emg2ps | emgj2
..... snipped .....
zeba   |  |               zeba
emr12  |   |          emr12
..... snipped .....
(note)  the second column can be empty

With the delimiter in place for the feed file, please let me know how to make it run..

I tried this, but not working ....

while read q1 q3 q5
  do [ "$q5" ] || { q5=$q4; q4=""; }
     /usr/lib/lpd/pio/etc/piomkjetd mkpq_jetdirect -p 'hplj-4000' -D pcl -q "$q1" -D ps -q "$q3" -h "$q5" -x '9100'   
  done <printfeed

Please advise.

IN WHAT WAY did it "not work"?

Sorry for the lack of explanations:

If I try this:

while read q1 q2 q3
  do [ "$q3" ] || { q3=$q2; q2=""; }
     /usr/lib/lpd/pio/etc/piomkjetd mkpq_jetdirect -p 'hplj-4000' -D pcl -q "$q1" -D ps -q "$q2" -h "$q3" -x '9100'   
  done <printfeed

with this feed file

cat printfeed
emg1  emg1ps  emgj1
emg2  emg2ps  emgj2
..... snipped .....
zeba                  zeba
emr12               emr12
..... snipped .....
(note)  the second column can be empty

I am getting :

0782-310 Usage:
        mkvirprt -A AttachmentType
        mkvirprt [ -A AttachmentType ] -d QueueDevice -n Device
         -q PrintQueue -s DataStream -t PrinterType [ -T ]
        Use 'smit mkvirprt' to use it interactively.

dspmsg: 1312-042 Invalid argument index in message.  May need
more arguments on the command line.

If I try this code:

while read q1 q3 q5
  do [ "$q5" ] || { q5=$q4; q4=""; }
     /usr/lib/lpd/pio/etc/piomkjetd mkpq_jetdirect -p 'hplj-4000' -D pcl -q "$q1" -D ps -q "$q3" -h "$q5" -x '9100'   
  done <printfeed

with this feed file (with delimiter 'pipe'):

at printfeed
emg1 | emg1ps | emgj1
emg2 | emg2ps | emgj2
..... snipped .....
zeba   |  |               zeba
emr12  |   |          emr12
..... snipped .....
(note)  the second column can be empty

I am getting this:

Host name | does not exist.  A valid host name is required!

So, the issue is the empty second column.

Please advise.

I can't see any relationship between your code snippet and the error msg that you get. Anyhow, might be it doesn't like an empty "$q2" - so, try

while read q1 q2 q3
  do [ "$q3" ] && P="-D ps -q $q2" || { q3=$q2; P=""; }
     /usr/lib/lpd/pio/etc/piomkjetd mkpq_jetdirect -p 'hplj-4000' -D pcl -q "$q1" "$P" -h "$q3" -x '9100'
  done <printfeed

If you are using | delimiters, you need to do while IFS="|" read ...