LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2007, 05:31 AM   #1
farkus888
Member
 
Registered: Oct 2006
Distribution: usually use arch
Posts: 103

Rep: Reputation: 15
converting shell while loop to for loop


I have a shell script that has a loop that passes through the lines in a file one at a time. the problem that I have is that the rsh commands inside the loop break it. some research shows that a for loop will not break with rsh commands like a while loop will. so my question is how can I best convert a loop of the form

Code:
while read line
do
stuff
done < file
to one of the form

Code:
for line in (lines of file)
do
stuff
done
this is a sh script on solaris 9. thanks in advance for your help.
 
Old 09-11-2007, 05:42 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986
A method could be: first set the input field separator as a newline to avoid problems with lines containing blanks, then...
Code:
#!/bin/sh
IFS="\n"
for line in `cat file`
do
  echo $line
done
You may also reset the IFS to the previous value (usually a blank space) by previously storing it in another (backup) variable.
Code:
IFS_PRE=$IFS
 
Old 09-11-2007, 06:38 PM   #3
farkus888
Member
 
Registered: Oct 2006
Distribution: usually use arch
Posts: 103

Original Poster
Rep: Reputation: 15
that sounds good until I run it and it breaks up my absolute filenames that are in the file we are iterating through on every n using the exact syntax you gave me. its breaking my absolute filenames into 3 or 4 garbled pieces of garbage.
 
Old 09-11-2007, 06:49 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 79
Quote:
Originally Posted by farkus888 View Post
that sounds good until I run it and it breaks up my absolute filenames that are in the file we are iterating through on every n using the exact syntax you gave me. its breaking my absolute filenames into 3 or 4 garbled pieces of garbage.
That’s because the IFS variable can’t be set using escape sequences (at least I don’t think so). You have to actually set the variable to a newline—e.g.:
Code:
IFS="$(echo)"
With the previous setting, IFS matches either a literal backslash or a literal “n” as the field separator.
 
Old 09-11-2007, 07:09 PM   #5
farkus888
Member
 
Registered: Oct 2006
Distribution: usually use arch
Posts: 103

Original Poster
Rep: Reputation: 15
yeah its now interpreting the backquoted section literally and breaking on each of the characters inside the quotes.
 
Old 09-11-2007, 07:20 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 79
Quote:
Originally Posted by farkus888 View Post
yeah its now interpreting the backquoted section literally and breaking on each of the characters inside the quotes.
Huh? Are you saying what I suggested does or doesn’t work?

Just FYI, on my machine this works as expected:
Code:
#!/bin/sh
IFS="$(echo)"
for line in `cat file`
do
  echo $line
done
This does not work (lines are broken on every literal lowercase “n”):
Code:
#!/bin/sh
IFS="\n"
for line in `cat file`
do
  echo $line
done
This happens to work, but I’m not sure if it’s portable:
Code:
#!/bin/sh
for line in `cat file`
do
  echo $line
done
 
Old 09-11-2007, 07:24 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
The default IFS is (iirc) (any/all of) space, tab and newline, but, as mentioned above, you have to set them literally, not via escape sequence.
Also, use single quote marks eg for newline its
IFS='
'

Of course, if the src file is just a list of absolute filenames, 1 per line, there's no need to fiddle with the IFS anyway.

Last edited by chrism01; 09-11-2007 at 07:25 PM.
 
Old 09-11-2007, 07:46 PM   #8
farkus888
Member
 
Registered: Oct 2006
Distribution: usually use arch
Posts: 103

Original Poster
Rep: Reputation: 15
looks like chrism01 wins with ignore it and the default FS works. I have a feeling I'll get stuck on something else along the way to finishing this project but for now I am set, thanks.
 
Old 09-12-2007, 02:30 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986
Quote:
Originally Posted by osor View Post
That’s because the IFS variable can’t be set using escape sequences
You're right, sorry! Indeed I tested on a file containing blank spaces and no n chars. I remember something about using octal codes, but not sure. I will check. Sorry again! Cheers
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
shell variables becoming zero outside the loop cool244 Programming 4 05-20-2006 03:33 PM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
[c shell] simple for loop saiz66 Programming 1 09-28-2004 07:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:56 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration