Trying to improve Bash scripting skills - question

Discussion in 'Mac Programming' started by Big Dave, May 27, 2010.

  1. Big Dave macrumors 6502

    Joined:
    Nov 27, 2007
    Location:
    Crestview, Fl
    #1
    I would like to learn how to perform a while loop while inside a while loop. I have written a small bit of script that I would love to learn to simplify. Any help is appreciated.
    -Thanks
    Dave

    Code:
    #!/bin/bash
    a="0"
    b="0"
    while [ $a -lt 10 ]
      do
        echo The a variable is $a and the b variable is $b.
        a=$[$a+1]
    done
    ##########
    a="0"
    b="5"
    while [ $a -lt 10 ]
      do
        echo The a variable is $a and the b variable is $b.
        a=$[$a+1]
    done
     
  2. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    Not really sure what your looking for

    Code:
    #!/bin/bash
    a=0
    while [ "$a" -lt 10 ]
      do
    
        b=0
        while [ "$b" -lt 10 ]
          do
              echo The a variable is $a and the b variable is $b.
              b=$[$b+1]
          done
    
        a=$[$a+1]
    done
    
    
     
  3. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #3
    If you want to get fancy...

    Code:
    #!/bin/bash
    a=0
    while [ $a -lt 10 ]
      do
    
        b=0
        while [ $b -lt 10 ]
          do
              echo The a variable is $a and the b variable is $b.
              (( b+=1 ))
          done
    
        (( a+=1 ))
    done
    
     
  4. Big Dave thread starter macrumors 6502

    Joined:
    Nov 27, 2007
    Location:
    Crestview, Fl
    #4
    When you see the solution, you have one of those: "Why didn't I think of that?" moments.
    Thanks for the help!

    Dave
     

Share This Page