1 2 Previous Next 16 Replies Latest reply on Apr 28, 2015 8:03 AM by Dipak Gaigole

    while + nexec

      Hi,

       

      Below script doesn't work:

       

      while read vm

      do

        echo ${vm}

        nexec app_serv nslookup ${vm} 2>&1 | sed "s/^/${vm};/g"

        echo $?

       

      done < //app_serv/tmp/vm_list

       

      the script stop running after one iteration despite vm_list file contains more than thousand line.

       

      When I try only echo lines (line by line) everything working.

      Am I making mistake or it's a bug?


      BSA: 8.6.00.216

       

      BR

      RD

        • 1. Re: while + nexec
          Santosh Kothuru

          Is the below command line independently working?

           

          nexec app_serv nslookup ${vm} 2>&1 | sed "s/^/${vm};/g"

          • 2. Re: while + nexec

            My snippet works for one line so the nexec command with nslookup works as well.

            The question is: why the loop is breaking after one iteration?

            • 3. Re: while + nexec
              Santosh Kothuru

              I see that you mentioned it is working for firest iteration and loop is not working for others.

              • 4. Re: while + nexec
                Bill Robinson

                Issue with invoking nexec in loop

                 

                 

                while read vm

                do

                  echo ${vm}

                  nexec -n app_serv nslookup ${vm} 2>&1 | sed "s/^/${vm};/g"

                  echo $?

                 

                done < //app_serv/tmp/vm_list

                • 5. Re: while + nexec
                  Yanick Girouard

                  while uses the internal IFS variable to define the characters it will use to break the list into items. If it's only looping once, it could be that the file you are feeding to the loop is not properly delimited.

                   

                  If you want to break on line feeds only, then do this:

                   

                  OLD_IFS=$IFS
                  IFS=$'\n'
                  while read vm; do
                    #your code here
                  done < //app_serv/tmp/vm_list
                  IFS=$OLD_IFS
                  
                  

                   

                  Then make sure your file in saved in Unix format and don't have any trailing or extra characters after each line.

                   

                  Just as a test, try doing just the echo of the $vm variable every loop. I would also add a sleep of 1-3 seconds at the end of the loop to prevent executing nexec too quickly.

                  • 6. Re: while + nexec

                    But why (below script):

                     

                    for item in `cat //app_serv/tmp/vm_list`

                    do

                      echo "item ${item}"

                      #nexec app_serv nslookup ${item} 2>&1 | sed "s/^/${item};/g"

                    done

                     

                    output:

                     

                    Info04/28/2015 12:02:43item x.223.250.8
                    Info04/28/2015 12:02:43item x.30.196.13
                    Info04/28/2015 12:02:43

                    item x.23.159.162

                    is working ?

                    • 7. Re: while + nexec
                      Yanick Girouard

                      Probably because in the second example, you're using cat to pass the contents of the file, but in the while loop you're simply feeding it to its input stream using <. It works differently (don't know what exactly is different, I just know it is).

                       

                      You could do this with while too like this:

                       

                      cat //app_serv/tmp/vm_list | while read item; do
                        echo "item ${item}"
                      done
                      
                      • 8. Re: while + nexec

                        My apologize I've pasted wrong script:

                         

                        Below script:

                        while read vm
                        do
                          echo "item ${vm}"
                        done < //app_serv/tmp/vm_list
                        

                         

                        gives output:

                         

                        Info04/28/2015 12:02:43item x.223.250.8
                        Info04/28/2015 12:02:43item x.30.196.13
                        Info04/28/2015 12:02:43

                        item x.23.159.162

                        So the problem is not with reading file line by line.

                        • 9. Re: while + nexec

                          This is working but it strange because when I use statement:

                          for item in `cat //app_serv/tmp/vm_list`

                          I may not have to use nexec with '-n' flag.





                          • 10. Re: while + nexec
                            Bill Robinson

                            the problem is w/ the nexec which the -n is meant to handle.

                             

                            run your script w/ a set -x and you should be able to see the return codes.  probably one of the nexec's in the loop is returning non-zero and causing the loop to exit like the other article mentions.

                            • 11. Re: while + nexec
                              Yanick Girouard

                              I would not have thought the -n switch would have fixed this behavior based on the description in the man page:

                               

                              -nLeave stdin alone  (do  not  read  any  data  from  stdin).  By
                              default,  nexec  will read all data it gets from stdin and sent
                              it to the remote command as standard input (stdin).  With  this
                              option  stdin  is not read and as such should only be used with
                              commands that do not require any input. See examples below.

                               

                              But then, when reading further down it explains it ...

                               

                              Notice in the next example the effect of the -n option.  In  the  first

                              instance, the first line of the stdin is read via the read host command

                              and the remaining entries are gobbled up by nexec and as such only  one

                              line  of output is generated.  In the second example all entries in the

                              file are handled as nexec is not reading stdin input.

                                      host% cat hosts

                                      madrid

                                      lisbon

                                      rome

                               

                                      host% cat hosts | while read host

                                      do

                                          echo -n "Hostname for $host is: "

                                          nexec $host hostname

                                      done

                                      Hostname for madrid is: madrid.bletch.com

                               

                               

                                      host% cat hosts | while read host

                                      do

                                          echo -n "Hostname for $host is: "

                                          nexec -n $host hostname

                                      done

                                      Hostname for madrid is: madrid.bletch.com

                                      Hostname for lisbon is: lisbon.bletch.com

                                      Hostname for rome is: rome.bletch.com

                              • 12. Re: while + nexec

                                Of course nexec with return code. And this may be explanation why while breaks once for continues looping.

                                 

                                But man nexec

                                 

                                -n      Leave stdin alone  (do  not  read  any  data  from  stdin).  By
                                              default,  nexec  will read all data it gets from stdin and sent
                                              it to the remote command as standard input (stdin).  With  this
                                              option  stdin  is not read and as such should only be used with
                                              commands that do not require any input.
                                
                                
                                              This option must also be used where nexec  is  intended  to  be
                                              used with commands like less or more.
                                

                                 

                                get me confused.

                                 

                                But let's summarize:

                                - for works/while breaks once error is occurred inside loop

                                - workaround for this is to use nexec -n structure

                                - i can't see relation between manual and workaround

                                 

                                BR

                                RD

                                • 13. Re: while + nexec
                                  Bill Robinson

                                  Yeah – i know, i’m just saying, that’s how to fix it ☺

                                   

                                  I’ll see about an answer as to what it’s doing and why it resolves this.  i got the answer a while ago but i don’t remember …

                                  • 14. Re: while + nexec
                                    Yanick Girouard

                                    I didn't catch it at first either (I learned too )... Read the man page of nexec completely, it explains it clearly using precisely your example. In short, unless you use -n, nexec gobbles up the remaining lines from stdin (standard input) that while read didn't grab in the first loop, so it only loops once because then while has nothing else to iterate through.

                                     

                                    So based on the example in the man page, if you have this input file:

                                     

                                    line1
                                    line2
                                    line3
                                    line4
                                    
                                    
                                    

                                    And use this code:

                                     

                                    while read line; do
                                         nexec $line hostname
                                    done < input_file
                                    
                                    
                                    

                                     

                                    Then this is what will happen:

                                     

                                    Loop 1:

                                    while read gobbles line1

                                    nexec gobbles up line2, line3, and line4 because it's passed to stdin

                                     

                                    Loop 2:

                                    while read has nothing left to iterate through (to read), so it ends.

                                     

                                    Now if you use -n, nexec will ignore the stdin, and will not gobble up the remaining lines, so while will always have something to iterate through until the file is done.

                                    1 2 Previous Next