Rearrange lists / "dynamic" elements count in for loop - possible?

Rearrange lists / "dynamic" elements count in for loop - possible?

am 16.04.2008 20:09:15 von naumfuieu2000

--------- code
#!/bin/bash

LIST="01 02 03 04 05 06 07 08 09 10"

for element in $LIST
do
workOn($element) # For each element do something
if [ "$?" -ne "0" ] # If the output from the last command/
function was not "success" = 0, then
then
export LIST="$LIST $element" # add the current element
to the bottom of the list, to try again later
fi
done
--------- code

That doesn't work. I suppose once bash reads the line "for element in
$LIST" it does the expansion and work on each element it read, from
that point does not matter the contents of $LIST, right?
I could use a while loop and keep trying to "workOn()" the current
element until it gives a exit status 0/success, but that will delay
the other elements.

Is that possible to dynamically change a for() loop? If so, is it
correct the way I'm working with lists?

This script notifies all registered users when a watched file is
altered, I connect directly to their mail hosts (resolve the MX for
the domain and telnet to it).

thanks

Re: Rearrange lists / "dynamic" elements count in for loop -

am 16.04.2008 20:27:37 von mop2

Using while:

#!/bin/bash

LIST="01 02 03 04 05 06 07 08 09 10"

LIST="$LIST ";NOW=
while [ "$LIST" ];do
NOW=${LIST%% *}
LIST=${LIST#* }
echo your_operation $NOW || LIST="$LIST $NOW" # append if error
done

Dont tested!



naumfuieu2...@yahoo.com.br wrote:
> --------- code
> #!/bin/bash
>
> LIST="01 02 03 04 05 06 07 08 09 10"
>
> for element in $LIST
> do
> workOn($element) # For each element do something
> if [ "$?" -ne "0" ] # If the output from the last command/
> function was not "success" = 0, then
> then
> export LIST="$LIST $element" # add the current element
> to the bottom of the list, to try again later
> fi
> done
> --------- code
>
> That doesn't work. I suppose once bash reads the line "for element in
> $LIST" it does the expansion and work on each element it read, from
> that point does not matter the contents of $LIST, right?
> I could use a while loop and keep trying to "workOn()" the current
> element until it gives a exit status 0/success, but that will delay
> the other elements.
>
> Is that possible to dynamically change a for() loop? If so, is it
> correct the way I'm working with lists?
>
> This script notifies all registered users when a watched file is
> altered, I connect directly to their mail hosts (resolve the MX for
> the domain and telnet to it).
>
> thanks

Re: Rearrange lists / "dynamic" elements count in for loop - possible?

am 17.04.2008 05:35:16 von Ed Morton

On 4/16/2008 1:09 PM, naumfuieu2000@yahoo.com.br wrote:
> --------- code
> #!/bin/bash
>
> LIST="01 02 03 04 05 06 07 08 09 10"
>
> for element in $LIST
> do
> workOn($element) # For each element do something
> if [ "$?" -ne "0" ] # If the output from the last command/
> function was not "success" = 0, then
> then
> export LIST="$LIST $element" # add the current element
> to the bottom of the list, to try again later
> fi
> done
> --------- code
>
> That doesn't work. I suppose once bash reads the line "for element in
> $LIST" it does the expansion and work on each element it read, from
> that point does not matter the contents of $LIST, right?
> I could use a while loop and keep trying to "workOn()" the current
> element until it gives a exit status 0/success, but that will delay
> the other elements.
>
> Is that possible to dynamically change a for() loop? If so, is it
> correct the way I'm working with lists?
>
> This script notifies all registered users when a watched file is
> altered, I connect directly to their mail hosts (resolve the MX for
> the domain and telnet to it).
>
> thanks

If I were you, I'd separate the re-tries from the initial list so you can add
controls on how many retry attempts to make, etc. if you like. Something like this:

LIST="01 02 03 04 05 06 07 08 09 10"
todo="$LIST"
while [ -n "$todo" ]
do
succ=""
fail=""
for element in $todo
do
workOn($element) # For each element do something
if [ "$?" -eq "0" ]; then
succ="$succ $element"
else
fail="$fail $element"
fi
done
echo "Succeeded:$succ"
echo "Failed:$fail"
todo="$fail"
done

would be very easy to instrument (and it'd work!). If you don't need to keep
LIST around after the loop, just use that directly instead of "todo".

Ed.