Need help with for loop with while read


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with for loop with while read
# 1  
Old 04-08-2009
Need help with for loop with while read

Hello,

The code below pulls printer information from a three-column file (set by the ${MULTIFILE} variable at the end), that has the printer queue name, IP, and type of printer. It is supposed to use this info to add the printers in the list to one, or more servers . The input file (${MULTIFILE} variable) looks like this:

Code:
queuename1 10.10.10.1  hp
queuename2 10.10.10.2 xerox
queuename3 192.168.1.3 hp

This is also the info that feeds the following:

while read QNAME PRTIP PTYPE


The problem I am running into is that it only executes on the first row of the input file, and doesn't go down the input file at all, and I believe this is due to the ${HNAME} variable, which is for the server. How would I make it so that I can have the list of printers installed on multiple servers?

Here is the code:

Code:
echo "Do you want to install these printers to the standard list of servers?"
echo "THIS COULD TAKE A WHILE!!"
print -n "Enter 'y' or 'n':"
read CHOICE
case "$CHOICE" in
         y|yes|Yes) HNAME=`cat servers.txt`
                    continue ;;
           n|no|No) echo

echo "Enter server host names separated by a blank space. ex.(server1 server2 server3):"
                    read HNAME
                    continue ;;
                 *) add_multiple_printer_func
                    exit ;;

while read QNAME PRTIP PTYPE
 do
  for i in $HNAME
   do
     if [ "$PTYPE" = "hp" -o "$PTYPE" = "Hp" -o "$PTYPE" = "HP" ] ; then
        PTYPE=257
    elif [ "$PTYPE" = "xerox" -o "$PTYPE" = "Xerox" -o "$PTYPE" = "XEROX" ] ; then
        PTYPE=9991
    elif [ "$PTYPE" = "unknown" -o "$PTYPE" = "Unknown" -o "$PTYPE" = "UNKNOWN" ] ; then
        PTYPE=9991
    fi
    if [ "$HNAME" = localhost ]
     then
        echo "Canceling print jobs for $QNAME on $HNAME"
        for j in `lpstat -o | grep -w $QNAME" | awk '{print $1}'`
        do
        echo ${j}
        cancel ${j}
        done
        /opt/hpnp/bin/removequeue -q$QNAME
        echo
        echo "/opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP"
        echo
        /opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP ; lpstat -p | grep $QNAME
#       echo "Sending test print to printer"
#       print "Printer succssfully installed on $HNAME" | lp -d $QNAME
     else
        echo $i
        echo "--------------------------------------------------------"
              echo "Canceling print jobs for $QNAME on $i"
           for j in `ssh ${i} "lpstat -o | grep -w $QNAME" | awk '{print $1}'`
             do
              echo ${j}
              echo "ssh ${i} cancel ${j}"
              ssh ${i} cancel ${j}
           done
   fi
done
  ssh $i "lpshut ; sleep 02 ; /opt/hpnp/bin/removequeue -q$QNAME ; sleep 02"
  echo
  echo "ssh $i /opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP"
  echo
  ssh $i "/opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP ; lpsched ; lpstat -p | grep $QNAME"
done < ${MULTIFILE} 2> tlg_printer.log 1>&2
sleep 04

Here is the output:

Code:
--------------------------------------------------------
Canceling print jobs for queuename1 on server1
lpshut: scheduler not running
queuename1 does not exist
 
ssh server1 /opt/hpnp/bin/addqueue -i 257 -q queuename1 -h 10.10.10.1
 
queuename1 added to spooler.
scheduler is running
printer queuename1 is idle.  enabled since Apr  8 01:18

After this output, it ends, and doesn't do the other printers.

Last edited by LinuxRacr; 04-08-2009 at 02:36 PM..
# 2  
Old 04-08-2009
I've had this before and it is a problem with nested while; do done loops.

A simple test would be to run:
Code:
while read QNAME PRTIP PTYPE; do
  echo QNAME = ${QNAME}
  echo PRTIP = ${PRTIP}
  echo PTYPE = ${PTYPE}
done < ${MULTIFILE}

Then but the next for or while loop in and so on to prove if the nesting of one of the loops is a loop too far!
# 3  
Old 04-08-2009
Here is a trace of the script running:

root@server> sh -x tlg_printer.sh 2>&1
Code:
+ clear
+ main_menu_func
/home/user/scripts
Wed Apr  8 14:05:02 CDT 2009
This script adds to, and deletes printers from HP-UX servers using Jetdirect.
There are also options to delete a queue, or queues only.


                  Printer Script Menu

                1. Add A Single Printer To One Or More Servers.
                   (Deletes the queue first if it already exists.)

                2. Delete A Single Printer From One Or More Servers.

                3. Add Multiple Printers To One Or More Servers.
                   (Deletes the queue first if it already exists.)

                4. Delete Multiple Printers From One Or More Servers.

                5.Exit

             V 1.6                              4/7/2009



Enter your selection:3
You will need a file of printers to read from that has three columns formatted as follows:
For example:
              QueueName  IPaddress      PrinterType(hp,xerox,unknown)
                 queue03  192.168.1.3  hp

The print queue name can only be 12 characters!!!
Please verify your list!!!
Do you have this file?
Enter 'y' or 'n':y

Enter the path to the input file: lists/Printer_List.txt
Do you want to install these printers to the standard list of servers?
THIS COULD TAKE A WHILE!!
Enter 'y' or 'n':n

Enter server host names separated by a blank space. ex.(server1 server2 server3):
server1

The script is running, and the menu will return when complete.
Please execute a 'tail -f' on the log file tlg_printer.log
in a separate window to see the output and error messages
scrolling, or view the logfile after the script finishes.


/home/user/scripts
Wed Apr  8 14:08:55 CDT 2009
This script adds to, and deletes printers from HP-UX servers using Jetdirect.
There are also options to delete a queue, or queues only.


                Printer Script Menu

                1. Add A Single Printer To One Or More Servers.
                   (Deletes the queue first if it already exists.)

                2. Delete A Single Printer From One Or More Servers.

                3. Add Multiple Printers To One Or More Servers.
                   (Deletes the queue first if it already exists.)

                4. Delete Multiple Printers From One Or More Servers.

                5.Exit

             V 1.6                              4/7/2009



Enter your selection:5

Here is the logfile output:

Code:
--------------------------------------------------------
Canceling print jobs for queuename1 on server1
lpshut: scheduler stopped
queuename1 does not exist
 
ssh server1 /opt/hpnp/bin/addqueue -i 257 -q queuename1 -h 192.168.1.3
 
queuename1 added to spooler.
scheduler is running
printer queuename1 is idle.  enabled since Apr  8 14:09


Last edited by LinuxRacr; 04-08-2009 at 04:26 PM..
# 4  
Old 04-10-2009
Ok, the script works now. The solution was to use one of the following solutions suggested by Dennis Handly, of the HP ITRC forums:

Code:
The first thing you could do is to throw everything away in your while loop and just print $QNAME $PRTIP $PTYPE.  
This should show you can read the whole file.  (TonyFullerMalv's suggestion)

The next thing you should do is gradually add back the contents of loop.

You'll come to the conclusion that you have been stabbed in the back by the remsh/ssh habit of eating up stdin.  
You'll need to use "remsh hostname -n" or "ssh hostname -n".  Or you can redirect stdin to /dev/null:
ssh ... < /dev/null

Or you could possibly use a hammer and stop this in its tracks by this design pattern:

while read X Y Z; do
(
   exec < /dev/null
   ...
)
done < file

I chose to use the last suggestion. Here is how it was used in my script:

Code:
echo "Do you want to install these printers to the standard list of servers?"
echo "THIS COULD TAKE A WHILE!!"
print -n "Enter 'y' or 'n':"
read CHOICE
case "$CHOICE" in
         y|yes|Yes) HNAME=`cat servers.txt`
                    continue ;;
           n|no|No) echo

echo "Enter server host names separated by a blank space. ex.(server1 server2 server3):"
                    read HNAME
                    continue ;;
                 *) add_multiple_printer_func
                    exit ;;

while read QNAME PRTIP PTYPE ; do
 (
  exec < /dev/null
  for i in $HNAME
   do
     if [ "$PTYPE" = "hp" -o "$PTYPE" = "Hp" -o "$PTYPE" = "HP" ] ; then
        PTYPE=257
    elif [ "$PTYPE" = "xerox" -o "$PTYPE" = "Xerox" -o "$PTYPE" = "XEROX" ] ; then
        PTYPE=9991
    elif [ "$PTYPE" = "unknown" -o "$PTYPE" = "Unknown" -o "$PTYPE" = "UNKNOWN" ] ; then
        PTYPE=9991
    fi
    if [ "$HNAME" = localhost ]
     then
        echo "Canceling print jobs for $QNAME on $HNAME"
        for j in `lpstat -o | grep -w $QNAME" | awk '{print $1}'`
        do
        echo ${j}
        cancel ${j}
        done
        /opt/hpnp/bin/removequeue -q$QNAME
        echo
        echo "/opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP"
        echo
        /opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP ; lpstat -p | grep $QNAME
#       echo "Sending test print to printer"
#       print "Printer succssfully installed on $HNAME" | lp -d $QNAME
     else
        echo $i
        echo "--------------------------------------------------------"
              echo "Canceling print jobs for $QNAME on $i"
           for j in `ssh ${i} "lpstat -o | grep -w $QNAME" | awk '{print $1}'`
             do
              echo ${j}
              echo "ssh ${i} cancel ${j}"
              ssh ${i} cancel ${j}
           done
   fi
done
  ssh $i "lpshut ; sleep 02 ; /opt/hpnp/bin/removequeue -q$QNAME ; sleep 02"
  echo
  echo "ssh $i /opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP"
  echo
  ssh $i "/opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP ; lpsched ; lpstat -p | grep $QNAME"
echo
 )
done < ${MULTIFILE} 2> tlg_printer.log 1>&2
sleep 04

Here is the output:

Code:
--------------------------------------------------------
Canceling print jobs for queuename1 on server1
lpshut: scheduler stopped
queuename1 does not exist
 
ssh server1 /opt/hpnp/bin/addqueue -i 257 -q queuename1 -h 192.168.1.3
 
queuename1 added to spooler.
scheduler is running
printer queuename1 is idle.  enabled since Apr  10 09:09

--------------------------------------------------------
Canceling print jobs for queuename2 on server1
lpshut: scheduler stopped
queuename2 does not exist
 
ssh server1 /opt/hpnp/bin/addqueue -i 257 -q queuename2 -h 192.168.1.4
 
queuename2 added to spooler.
scheduler is running
printer queuename2 is idle.  enabled since Apr  10 09:09

--------------------------------------------------------
Canceling print jobs for queuename3 on server1
lpshut: scheduler stopped
queuename3 does not exist
 
ssh server1 /opt/hpnp/bin/addqueue -i 257 -q queuename3 -h 192.168.1.5
 
queuename3 added to spooler.
scheduler is running
printer queuename3 is idle.  enabled since Apr  10 09:10


Last edited by LinuxRacr; 04-10-2009 at 05:52 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While read line loop

Based on text file: PATH:/media/hdd/Media/Video/title1 FILE:/media/cache/281662-14.jpg PATH:/media/hdd/Media/Video/title2 FILE:/media/cache/281662-15.jpg PATH:/media/hdd/Media/Video/title3 FILE:/media/cache/281662-16.jpg PATH:/media/hdd/Media/Video/title4 FILE:/media/cache/281662-17.jpg... (12 Replies)
Discussion started by: TiedCone
12 Replies

2. Shell Programming and Scripting

Help with while read loop

Hey all, Tried searching the forums but my search-fu may not be strong today; please feel free to redirect me if I have simply missed a post that would be helpful! Trying to create a while loop that reads a list of configuration files, checks for a line that starts with "SOME_CMD" and ends... (4 Replies)
Discussion started by: jdwyer
4 Replies

3. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

4. Shell Programming and Scripting

until loop and read

Hi guys what I wanna do is to create a script where can I input several times a file - assume with read function that will be inserted into a temp. file for further processing. When I press q I want that the loop will stop and continue in my script I typed this but the options q is not working... (6 Replies)
Discussion started by: kl1ngac1k
6 Replies

5. Shell Programming and Scripting

grep within while read loop

I have a key file $ cat klist 5 N:8855 CASA VERDE ROAD :32827 :ORLAND 5 N:585 MOLLY LANE :30189 :WOODST 5 N:320 NINA ROAD :32304 :TALLAH and a data file, see example of the line below: N:RT 15 & N 7TH STREET :17837 :U SAVE I need to search by key (2nd field) from klist... (6 Replies)
Discussion started by: migurus
6 Replies

6. UNIX for Dummies Questions & Answers

if elif loop with read

I am trying to setup an if, elif statement that is dependant on a variable. If the user does not enter a valid option I would like it to prompt them again and start the loop over. Here is what I have so far: echo -n "enter variable (a, b, or c): " read freq if ; then echo "a" elif ;... (2 Replies)
Discussion started by: BStanley346
2 Replies

7. Shell Programming and Scripting

read line in a for loop

Hi All, How can we use read line using the index value of a FOR loop? eg: pt_mstr,pt_status,8 pt_mstr,pt_buyer,8 pt_mstr,pt_sfty_stk,8 pt_mstr,pt_ord_pol,3 pt_mstr,pt_capacity,8 pt_mstr,pt_plan_ord,3 pt_mstr,pt_ord_mult,8 From this file i want to read the line2, 3 and 4 only using a FOR... (3 Replies)
Discussion started by: balajim
3 Replies

8. Shell Programming and Scripting

Need Help : while read loop

hi all, Can you please help me with this issue ? while ####infinite loop do cat file1 |while read var1 var2 do func1 $var1 $var2 done cat file2 | while read var11 var22 do func2 $var11 $var22 done done func1 is called till all the values are read... (2 Replies)
Discussion started by: Navatha
2 Replies

9. Shell Programming and Scripting

Please explain read in a while loop

I have a script which tries to read input from a user for every value read from a file. The input file is #> more testfile TEST1 | D200 | 12345601 | | ABC company | m TEST2 | D201 | 12345602 | | ABC company | m The script test.sh is as follows while read line do read test?"Enter a... (5 Replies)
Discussion started by: jerardfjay
5 Replies

10. UNIX for Dummies Questions & Answers

read inside a while loop

Hi all, In a while loop, like below... while read line do read choice case $choice in 1) echo "xxx" esac done < file why I can't run the read choice???? (3 Replies)
Discussion started by: dta4316
3 Replies
Login or Register to Ask a Question