Results 1 to 4 of 4

Thread: Looping problems

  1. #1
    Join Date
    Mar 2014
    Beans
    5

    Looping problems

    A hello to all the forum I'm new here c:

    The very first of all sorry for my english It's not the first, the second or the twentieth time I come here but I finally registered c: Congrats and thanks for all to contribute in the forum

    Well so the problem is that; I'm starting on /bin/bash and I need to loop inside a loop while looping.
    What I mean is, the entire code is inside a while, and I need to loop inside a while on there, what I mean is:

    #!/bin/bash

    while [ a -ge b ]; do
    while [ c -eq d ]; do
    while [ e -ne f ]; do
    done
    done
    done

    When I use only two, I usually do with "[[ ... ]]" and it works, but I cannot find how to loop again.
    Thanks~

  2. #2
    Join Date
    Mar 2014
    Beans
    8

    Talking Nested while loop not possible

    Bash can't nested while loops like this. Other languages can this. In bash you have to create something like that:

    Code:
    while [ ... ]
    do 
    
    var=`while [ ... ] ; do ; somestuff ; done`
    
    done
    Ehm, special note this: ` is not like '
    The ` means, that the code inside can be run!


  3. #3
    Join Date
    Mar 2014
    Beans
    5

    Re: Looping problems

    Well that's a problem... Then I will try the same on Java ... Thanks for help

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Looping problems

    This works fine for me

    Code:
    $ cat looploop.sh
    #!/bin/bash
    
    j=0
    while [ $j -lt 3 ]; do
      i=0
      while [ $i -lt 5 ]; do
        printf "j = %d, i = %d\n" $j $i
        ((i++))
      done
      ((j++))
    done
    Code:
    $ ./looploop.sh
    j = 0, i = 0
    j = 0, i = 1
    j = 0, i = 2
    j = 0, i = 3
    j = 0, i = 4
    j = 1, i = 0
    j = 1, i = 1
    j = 1, i = 2
    j = 1, i = 3
    j = 1, i = 4
    j = 2, i = 0
    j = 2, i = 1
    j = 2, i = 2
    j = 2, i = 3
    j = 2, i = 4
    IIRC bash 'while' loops implicitly do get executed as subshells - hence why 'exit' doesn't always do what people expect...

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •