Skip to main content
deleted 26 characters in body
Source Link
Rui F Ribeiro
  • 58k
  • 28
  • 156
  • 237

Newbie at bash scripting. I'm trying to make a basic loop, and I want to exit the nested loop when I loop the parent. The nested loop should restart each time the parent loops.

#!/bin/bash

loop1=0
i=0

while [[ $loop1 -eq "0" ]]; do

    let "i+=1"
    echo $i "--------------"

    loop2=0
    j=0

    while [[ $loop2 -eq "0" ]]; do

        let "j+=1"
        echo $j $i
        sleep .2    

    done &

    loop2=1
    sleep 2

done

My desired output would look like this:

1 --------------
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
2 --------------
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
10 2

I'm getting that output, sort of, but the original nested loop doesn't terminate, so I also get 11 1, 12 1, 13 1, and so on.

I'm sure this is dead simple, but I'm not getting it.

Newbie at bash scripting. I'm trying to make a basic loop, and I want to exit the nested loop when I loop the parent. The nested loop should restart each time the parent loops.

#!/bin/bash

loop1=0
i=0

while [[ $loop1 -eq "0" ]]; do

    let "i+=1"
    echo $i "--------------"

    loop2=0
    j=0

    while [[ $loop2 -eq "0" ]]; do

        let "j+=1"
        echo $j $i
        sleep .2    

    done &

    loop2=1
    sleep 2

done

My desired output would look like this:

1 --------------
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
2 --------------
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
10 2

I'm getting that output, sort of, but the original nested loop doesn't terminate, so I also get 11 1, 12 1, 13 1, and so on.

I'm sure this is dead simple, but I'm not getting it.

I'm trying to make a basic loop, and I want to exit the nested loop when I loop the parent. The nested loop should restart each time the parent loops.

#!/bin/bash

loop1=0
i=0

while [[ $loop1 -eq "0" ]]; do

    let "i+=1"
    echo $i "--------------"

    loop2=0
    j=0

    while [[ $loop2 -eq "0" ]]; do

        let "j+=1"
        echo $j $i
        sleep .2    

    done &

    loop2=1
    sleep 2

done

My desired output would look like this:

1 --------------
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
2 --------------
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
10 2

I'm getting that output, sort of, but the original nested loop doesn't terminate, so I also get 11 1, 12 1, 13 1, and so on.

I'm sure this is dead simple, but I'm not getting it.

Source Link

Kill a nested while loop

Newbie at bash scripting. I'm trying to make a basic loop, and I want to exit the nested loop when I loop the parent. The nested loop should restart each time the parent loops.

#!/bin/bash

loop1=0
i=0

while [[ $loop1 -eq "0" ]]; do

    let "i+=1"
    echo $i "--------------"

    loop2=0
    j=0

    while [[ $loop2 -eq "0" ]]; do

        let "j+=1"
        echo $j $i
        sleep .2    

    done &

    loop2=1
    sleep 2

done

My desired output would look like this:

1 --------------
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
2 --------------
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
10 2

I'm getting that output, sort of, but the original nested loop doesn't terminate, so I also get 11 1, 12 1, 13 1, and so on.

I'm sure this is dead simple, but I'm not getting it.