LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-14-2011, 03:44 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,033

Rep: Reputation: 45
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?

Last edited by stf92; 06-14-2011 at 03:45 AM.
 
Old 06-14-2011, 04:44 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
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.
 
Old 06-14-2011, 05:13 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,033

Original Poster
Rep: Reputation: 45
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.

Last edited by stf92; 06-14-2011 at 05:17 AM.
 
Old 06-14-2011, 05:19 AM   #4
ssrameez
Member
 
Registered: Oct 2006
Location: bangalore
Distribution: Fedora, Ubuntu, Debian, Redhat
Posts: 82

Rep: Reputation: 6
than using > use string comparison operators like eq or ne
 
Old 06-14-2011, 05:23 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
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?
 
1 members found this post helpful.
Old 06-14-2011, 06:00 AM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,033

Original Poster
Rep: Reputation: 45
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
 
Old 06-14-2011, 06:31 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
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
 
1 members found this post helpful.
Old 06-14-2011, 07:12 AM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,033

Original Poster
Rep: Reputation: 45
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.

Last edited by stf92; 06-14-2011 at 07:14 AM.
 
Old 06-14-2011, 09:16 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash:Printing the line number in bash script suryaemlinux Programming 2 02-05-2011 09:59 AM
[SOLVED] bash for loop iteration question. ncalsmitty1369 Programming 4 02-05-2010 05:50 PM
eth0: too many iterations (6) in nv_nic_irq bee2643 Linux - Networking 1 01-10-2009 11:47 PM
thread creation fails after a number of iteration sudevank Programming 1 05-26-2008 09:45 AM
How do I stop redhat bash colors? they are breaking my balls! gtjmn308 Linux - Software 2 09-16-2006 09:41 PM


All times are GMT -5. The time now is 08:50 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration