LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Breaking iteration after a given number of iterations (bash). (http://www.linuxquestions.org/questions/linux-newbie-8/breaking-iteration-after-a-given-number-of-iterations-bash-886207/)

stf92 06-14-2011 03:44 AM

Breaking iteration after a given number of iterations (bash).
 
Hi:

Take this block of pseudocode:
Code:

for i in $list; do
    <some statements>
    if i > 5
        exit
    <more statements>
done

That is, want to terminate the iteration just after 5 iterations. A first problem, is that list is a string and not a number. It's all I can say, except to keep reading bash monster manual. Any hint?

grail 06-14-2011 04:44 AM

Well you should think a bit more about the things you are trying to accomplish. You already have all the information you need.

1. $list is a list of stings as indicated by you
2. i must be a number if compared to one (generally)
3. break is the command to call to simply break the loop. IMHO it is never a good idea to exit an entire script from the middle of an operation like a loop.

So using this information, restructure the script so items from step 1 and 2 are the correct types.

stf92 06-14-2011 05:13 AM

Hi and thanks for your kind reply.

I think 5 must be the correct type. That is, instead of 5 I must write the 5th string in variable called list. Unfortunately this string consists of more almost 80 chars.

There must be some other way for I do not thing there is a numeric type in bash. If the latter were true then a counter would solve the problem. Regards.

ssrameez 06-14-2011 05:19 AM

than using > use string comparison operators like eq or ne

grail 06-14-2011 05:23 AM

Quote:

I think 5 must be the correct type. That is, instead of 5 I must write the 5th string in variable called list.
Yes your counter will be an integer that is compared to the number 5.
Quote:

Unfortunately this string consists of more almost 80 chars.
This has me concerned as your for loop will not split a single string as it stands??
For example:
Code:

#!/bin/bash

list=one_two_three_four

for i in $list
do
        echo $i
done

The output when this script is run is a single line with the string 'one_two_three_four' displayed

Standard variables are typically string or numeric depending on what you assign to them:
Code:

line="string"

num=10

Let me know if this helps you?

stf92 06-14-2011 06:00 AM

Your post greatly helps me, alghough I'm a bit confused about your statement:
Quote:

This has me concerned as your for loop will not split a single string as it stands??
If you do 'locate mimeTypes.rdf' you'll find the file names listed by locate (I mean full names) are pretty long. In the skeleton program shown in post #1, list is in fact the output of locate:
Code:

list=$(locate mimeTypes.rdf)
And doing 'echo $list' I can see only spaces separating the full file names, which seems correct. Was that your concern?

I do not, please, want you to believe I want the full script running correctly by your intervention. But I think the best think I can do to give you the right picture is sending you it. Here it is.
Code:

#!/bin/bash
# Sobre un ejemplo de INTRO_LI.TXT de M.Garrels, 7.2.5

# Copia los archivos listados por el locate a la manera indicada en
# manual de cp, opcion --parents. Alli, full file name es, e.g.,
# /usr/bin/mplayer.

LIST=$(locate mimeTypes.rdf)
for i in $LIST; do
        cp --parents $i /xp/borrar/
done


grail 06-14-2011 06:31 AM

Quote:

list=$(locate mimeTypes.rdf)
This now makes more sense. Also, be wary that is any of your paths have spaces in them that this approach will not work.

To maybe help you further (I know I am not giving you direct answers here but am trying to help you learn as opposed to just telling you the answer),
your pseudo code would look like:
Code:

set a counter equal to 0

for each item in the list
do
  some tasks here
  increase the counter
  test if the counter has reached objective amount (5 in your example)
      if true, exit the for loop

  perform some more tasks
exit loop as all items read from list


stf92 06-14-2011 07:12 AM

Thanks for your post again, grail.

About spaces interspersed in file names --what a fine occurrence to have provided for this! I say it ironically, I have something posted in my threads and a little thinking plus trial and error will give the answer.

I think I have now all the elements to set to work. Which is what I'll do and, in case of any doubt, but remembering

Quote:

Well you should think a bit more about the things you are trying to accomplish.
I can always fall back on LQ. Really, I'm under the impression of having just left the classroom, which usually has been a good one for me. Kind regards.

grail 06-14-2011 09:16 AM

Well I am glad I could help :) Please post your final solution so others can also learn from the process. (Also we might be able to provide further tips once you have a working solution)
Quote:

Really, I'm under the impression of having just left the classroom, which usually has been a good one for me.
I am very pleased to see you took our interaction positively :)


All times are GMT -5. The time now is 03:07 AM.