Passing three variables to loop and error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing three variables to loop and error
# 1  
Old 09-28-2014
Question 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:

Code:
/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.

HTML Code:
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.

Code:
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:

PHP Code:
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!
# 2  
Old 09-28-2014
replace:
Code:
q1 = cat $pqn |awk '{print $1}'

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

# 3  
Old 09-28-2014
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:

Code:
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

# 4  
Old 09-28-2014
If the second value can be empty in your input file, use Corona688's proposal and adapt:
Code:
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

# 5  
Old 09-28-2014
RudiC & Corona688,

It works charming except one error:

HTML Code:
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
HTML Code:
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 -- |):

HTML Code:
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 ....

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

Please advise.
# 6  
Old 09-28-2014
Moderator's Comments:
Mod Comment Posting "Does not work" without explanation does not help you or anyone. If a command does not work for you, please show the exact circumstances you used it, and the exact error or malfunction you received. Do not paraphrase errors, or post the text as links, images, or attachments if you can avoid it: Paste the exact message, in code tags, like [code] text [/code] or by selecting the text and using the Image button.

Thank you.

The UNIX and Linux Forums


IN WHAT WAY did it "not work"?
# 7  
Old 09-28-2014
Sorry for the lack of explanations:

If I try this:
Code:
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
PHP Code:
cat printfeed
emg1  emg1ps  emgj1
emg2  emg2ps  emgj2
..... snipped .....
zeba                  zeba
emr12               emr12
..... snipped .....
(
note)  the second column can be empty 
I am getting :

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

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

If I try this code:

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'):

PHP Code:
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:

PHP Code:
Host name does not exist.  A valid host name is required
So, the issue is the empty second column.

Please advise.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh While Loop - passing variables to functions

I'm reading a text file using a while loop but when I call a function from within this loop it exits that same iteration … even though there are many more lines in the file to be read. I thought it was something to do with the IFS setting but it appears that a function call (when run... (3 Replies)
Discussion started by: user052009
3 Replies

2. UNIX for Dummies Questions & Answers

Passing variables to mkuser

In a ksh, I'm attempting to pass my string of arguments to the mkuser command in a variable as follows... cmd="pgrp=ACRGENU groups=ACRGENU home=/home/${USERID} shell=/usr/bin/ksh" cmd=$cmd" gecos='${USERNAME}' login=true su=false rlogin=true daemon=true" cmd=$cmd" admin=false... (4 Replies)
Discussion started by: bubbawuzhere
4 Replies

3. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

4. Shell Programming and Scripting

Passing Variables

I have below data: DAY1=10202013 I am trying below but not getting the desired output: COUNT=1 DATE=DAY$COUNT echo "Date is $DATE" However output I am getting is: Date is DAY1 I wanted the output as: Date is 10202013 I tried following as well: DAY1=10202013 COUNT=1... (3 Replies)
Discussion started by: rockyr1985
3 Replies

5. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

6. Shell Programming and Scripting

Passing 2 variables

Hi All, I need to pass 2 variables name 'vamskt' and 'vamsi'. Here is my question: delete from gpi.usergroup where usg_user_id in ('vamskt'); delete from gpi.userroles where uro_user_id in ('vamskt'); delete from gpi.user where usr_id in ('vamskt'); insert into gpi.user... (3 Replies)
Discussion started by: tvamsikiran
3 Replies

7. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. UNIX for Dummies Questions & Answers

Passing variables between scripts...

Hey all, I'm wondering how you pass variable's that are defined in one script to another script that's been called by that first script..... Best regards, Jaz (1 Reply)
Discussion started by: Jazmania
1 Replies

9. Shell Programming and Scripting

Passing variables to sed

Hi folks, I'm looking for a solution to pass variables to a sed-command. I'm reading a lot of threats and also the q&a "How can I use a variable in sed?". None of these commands works. I'm using AIX 5.2. I want to do the following: NUMBER=` echo 38341` | sed -n '/$NUMBER/p' an obtained... (3 Replies)
Discussion started by: jfisch
3 Replies

10. Shell Programming and Scripting

passing variables

Hi, Is there any way to pass variable to a sed script.For awk we have -v option.like that do we have any way to pass variable to a sed script from a awk script or from normal script? Thanx, sounder (1 Reply)
Discussion started by: sounder123
1 Replies
Login or Register to Ask a Question