Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Round-robin loop among one group of digits based on certain condition.

15 views
Skip to first unread message

Hongyi Zhao

unread,
Feb 15, 2015, 10:34:36 PM2/15/15
to
Hi all,

I want to do the following thing:

-------
#!/bin/bash
i=1
until [[ The_condition_is_fulfilled. ]]
do

Do_the_things_I_want_to_do_here.
let "i+=1"
if i > some digit, say 6, and the condition for until is
unfulfilled, do the loop from 1 again.

done
-------

Any hints?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Kaz Kylheku

unread,
Feb 15, 2015, 11:46:28 PM2/15/15
to
On 2015-02-16, Hongyi Zhao <hongy...@gmail.com> wrote:
> Hi all,
>
> I want to do the following thing:
>
> -------
> #!/bin/bash
> i=1
> until [[ The_condition_is_fulfilled. ]]
> do
>
> Do_the_things_I_want_to_do_here.
> let "i+=1"
> if i > some digit, say 6, and the condition for until is
> unfulfilled, do the loop from 1 again.
>
> done
> -------
>
> Any hints?

Yes.

1. Bash has no "until". Since your "until" has a top-of loop test,
evidently, simply reverse the condition:

while [ the condition is not fulfilled ] ; do
..
done

2. "let i+1" can be done in various ways. Here are two:

i=$((i + 1))

: $((i++))

The second one is the shell's "null command", which is spelled as a colon.
The null command expands and evaluates its arguments, but does nothing.
Here argument $((i++)) is an arithmetic expansion with the side
effect of incrementing i.

3. The "and the condition for until is unfulfilled" in the last step
is superfluous. Simply wrap i back to 1. Then let the loop's top test
evaluate whether the condition is unfulfilled.

(However, the correctness of this depends on whether subsequent code depends
on the value of i.)

Kaz Kylheku

unread,
Feb 15, 2015, 11:48:33 PM2/15/15
to
On 2015-02-16, Kaz Kylheku <k...@kylheku.com> wrote:
> 1. Bash has no "until". Since your "until" has a top-of loop test,

Yes it does, and it's POSIX. Sorry about that!

Chris F.A. Johnson

unread,
Feb 16, 2015, 12:08:11 AM2/16/15
to
On 2015-02-16, Hongyi Zhao wrote:
> Hi all,
>
> I want to do the following thing:
>
> -------
> #!/bin/bash
> i=1
> until [[ The_condition_is_fulfilled. ]]
> do
>
> Do_the_things_I_want_to_do_here.
> let "i+=1"
> if i > some digit, say 6, and the condition for until is
> unfulfilled, do the loop from 1 again.
>
> done
> -------
>
> Any hints?

What part of it is giving you problems?

--
Chris F.A. Johnson

Hongyi Zhao

unread,
Feb 16, 2015, 1:55:45 AM2/16/15
to
On Mon, 16 Feb 2015 04:46:22 +0000, Kaz Kylheku wrote:

> Simply wrap i back to 1. Then let the loop's top test
> evaluate whether the condition is unfulfilled.

What's the code line for this?

Hongyi Zhao

unread,
Feb 16, 2015, 1:58:07 AM2/16/15
to
On Sun, 15 Feb 2015 23:58:53 -0500, Chris F.A. Johnson wrote:

>> if i > some digit, say 6, and the condition for until is
>> unfulfilled, do the loop from 1 again.

I don't know how to achieve the above purpose in my script.

>>
>> done
>> -------
>>
>> Any hints?
>
> What part of it is giving you problems?





--

Dave Farrance

unread,
Feb 16, 2015, 4:00:48 AM2/16/15
to
Easily forgotten because bash's
until [[ expr ]] ... is the same as ... while ! [[ expr ]]
which makes it a bit redundant, so...

while :; do
...
[[ expr ]] && break
done

Dave Farrance

unread,
Feb 16, 2015, 4:19:48 AM2/16/15
to
Hongyi Zhao <hongy...@gmail.com> wrote:

>On Sun, 15 Feb 2015 23:58:53 -0500, Chris F.A. Johnson wrote:
>
>>> if i > some digit, say 6, and the condition for until is
>>> unfulfilled, do the loop from 1 again.
>
>I don't know how to achieve the above purpose in my script.

That's still unclear. If that condition is NOT fufilled, what do you want
to do? Exit the loop? Continue the loop? If it's the latter then all
you want to do is reset i to 1:

(( i = > 6 )) & i=1

Dave Farrance

unread,
Feb 16, 2015, 4:21:08 AM2/16/15
to
Sorry
(( i = > 6 )) && i=1

Dave Farrance

unread,
Feb 16, 2015, 4:21:45 AM2/16/15
to
FFS!
(( i > 6 )) && i=1

Janis Papanagnou

unread,
Feb 16, 2015, 6:12:55 AM2/16/15
to
Am 16.02.2015 um 04:34 schrieb Hongyi Zhao:
> Hi all,
>
> I want to do the following thing:
>
> -------
> #!/bin/bash
> i=1
> until [[ The_condition_is_fulfilled. ]]
> do
>
> Do_the_things_I_want_to_do_here.
> let "i+=1"
> if i > some digit, say 6, and the condition for until is
> unfulfilled, do the loop from 1 again.
>
> done
> -------
>
> Any hints?

As mentioned by others your request is not unambiguous. For one
interpretation...

until [[ The_condition_is_fulfilled ]]
do
for (( i=1; i<=6; i++ ))
do
Do_the_things_I_want_to_do_here
done
done

Depending on whether 'The_condition_is_fulfilled' should also
be checked in the inner loop or not you can add it there, either
to the i<=6 check or as an explicit test with a break statement
(as already shown by others).

Janis

>
> Regards
>

Hongyi Zhao

unread,
Feb 16, 2015, 7:39:01 AM2/16/15
to
On Mon, 16 Feb 2015 09:19:42 +0000, Dave Farrance wrote:

> That's still unclear. If that condition is NOT fufilled, what do you
> want to do? Exit the loop? Continue the loop?

Yes, if that condition is NOT fulfilled, continue the loop. If i > some
digits, say 6, and the condition is NOT fulfilled still, rerun the loop
from i=1 on the things that I want to do.

Regards

Hongyi Zhao

unread,
Feb 16, 2015, 7:43:45 AM2/16/15
to
On Mon, 16 Feb 2015 12:12:51 +0100, Janis Papanagnou wrote:

>> if i > some digit, say 6, and the condition for until is
>> unfulfilled, do the loop from 1 again.
>>
>> done
>> -------
>>
>> Any hints?
>
> As mentioned by others your request is not unambiguous.

As I've posted to Dave, I mean the following logic:

-----------
If that condition is NOT fulfilled, continue the loop. If i > some
digits, say 6, and the condition is NOT fulfilled still, rerun the loop
from i=1 on the things that I want to do.
-----------

Does the ambiguity in my wording eliminated this time?

Janis Papanagnou

unread,
Feb 16, 2015, 7:59:26 AM2/16/15
to
Am 16.02.2015 um 13:43 schrieb Hongyi Zhao:
> On Mon, 16 Feb 2015 12:12:51 +0100, Janis Papanagnou wrote:
>
>>> if i > some digit, say 6, and the condition for until is
>>> unfulfilled, do the loop from 1 again.
>>>
>>> done
>>> -------
>>>
>>> Any hints?
>>
>> As mentioned by others your request is not unambiguous.
>
> As I've posted to Dave, I mean the following logic:
>
> -----------
> If that condition is NOT fulfilled, continue the loop. If i > some
> digits, say 6, and the condition is NOT fulfilled still, rerun the loop
> from i=1 on the things that I want to do.
> -----------
>
> Does the ambiguity in my wording eliminated this time?

It's unclear how in the context of your
"and the condition for until is unfulfilled"
you want to handle the "and if not - what then?" case.

Maybe (or maybe not) all you want is a circular counter...

i=$(( (i+1)%6 ))

instead of the 'let' and the 'if' commands in your original code.

Janis

>
> Regards
>

Janis Papanagnou

unread,
Feb 16, 2015, 8:03:36 AM2/16/15
to
Or (if the index shall run from 1 to 6) rather...

i=$(( i%6 + 1 ))

Dave Farrance

unread,
Feb 16, 2015, 8:54:43 AM2/16/15
to
Hongyi Zhao <hongy...@gmail.com> wrote:

>On Mon, 16 Feb 2015 09:19:42 +0000, Dave Farrance wrote:
>
>> That's still unclear. If that condition is NOT fufilled, what do you
>> want to do? Exit the loop? Continue the loop?
>
>Yes, if that condition is NOT fulfilled, continue the loop. If i > some
>digits, say 6, and the condition is NOT fulfilled still, rerun the loop
>from i=1 on the things that I want to do.

So there's no loop-end exit condition, just a manipulation of the loop
variable?

e.g. add digits 1-6 to a string until it's 20 characters long:

#!/bin/bash
j=1 s=''
until (( ${#s} == 20 )); do
s=$s$j
(( j = j%6 + 1 ))
done
echo $s

12345612345612345612

Hongyi Zhao

unread,
Feb 16, 2015, 8:47:07 PM2/16/15
to
On Mon, 16 Feb 2015 13:54:39 +0000, Dave Farrance wrote:

> So there's no loop-end exit condition, just a manipulation of the loop
> variable?

The loop-end exit condition depended on the argument of until, which is
not necessarily has relation with j, the loop-index number. While the
loop-index is a round-robin loop among one group of digits, just as you
have described in the following example, i.e., making a round trip on
digits 1-6 for 20 characters long should be like the this for my case:

12345612345612345612

But, for my case, I didn't know the length of the digits characters which
used for the loop-index number, because the only loop-end exit condition
is controlled by the argument of until, which, as mentioned above, is not
necessarily has relation with loop-index number.

Hongyi

>
> e.g. add digits 1-6 to a string until it's 20 characters:
>
> #!/bin/bash j=1 s=''
> until (( ${#s} == 20 )); do
> s=$s$j (( j = j%6 + 1 ))
> done echo $s
>
> 12345612345612345612





Janis Papanagnou

unread,
Feb 17, 2015, 6:53:56 AM2/17/15
to
Am 17.02.2015 um 02:47 schrieb Hongyi Zhao:
> On Mon, 16 Feb 2015 13:54:39 +0000, Dave Farrance wrote:
>
>> So there's no loop-end exit condition, just a manipulation of the loop
>> variable?
>
> The loop-end exit condition depended on the argument of until, which is
> not necessarily has relation with j, the loop-index number. While the
> loop-index is a round-robin loop among one group of digits, just as you
> have described in the following example, i.e., making a round trip on
> digits 1-6 for 20 characters long should be like the this for my case:
>
> 12345612345612345612
>
> But, for my case, I didn't know the length of the digits characters which
> used for the loop-index number, because the only loop-end exit condition
> is controlled by the argument of until, which, as mentioned above, is not
> necessarily has relation with loop-index number.

Dave's 'until' contition and string concatenation is a *sample* (since
you did not mention any concrete condition nor what your loop actually
does). Is that so hard to understand?! I'm sure all you need is already
there, you only have to add "add one and one" (no, not literally!) to
get your code from all the input you got in this thread.

With your informal code and the suggestion I (and Dave) gave yesterday:

-------
#!/bin/bash
i=1
until [[ The_condition_is_fulfilled. ]]
do

Do_the_things_I_want_to_do_here.

i=$(( i%6 + 1 ))

done
-------

You asked for hints and got a lot.

If you neither can accurately clarify your issue nor explain in what way
the given suggestions are deviating from what you expect I'll bail out.

Good luck!

Janis

Hongyi Zhao

unread,
Feb 17, 2015, 7:15:57 AM2/17/15
to
On Tue, 17 Feb 2015 12:53:49 +0100, Janis Papanagnou wrote:

> You asked for hints and got a lot.
>
> If you neither can accurately clarify your issue nor explain in what way
> the given suggestions are deviating from what you expect I'll bail out.

Thanks a lot, in fact, this thread is a part of issue of my another
problem, which still haven't solved yes. So I just give a not so concrete
description in this thread. Thanks for all of the help from you Dave and
others.

Regards
0 new messages