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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-11-2007, 05:31 AM
|
#1
|
Member
Registered: Oct 2006
Distribution: usually use arch
Posts: 103
Rep:
|
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.
|
|
|
09-11-2007, 05:42 AM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
|
|
|
09-11-2007, 06:38 PM
|
#3
|
Member
Registered: Oct 2006
Distribution: usually use arch
Posts: 103
Original Poster
Rep:
|
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.
|
|
|
09-11-2007, 06:49 PM
|
#4
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
Quote:
Originally Posted by farkus888
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.:
With the previous setting, IFS matches either a literal backslash or a literal “n” as the field separator.
|
|
|
09-11-2007, 07:09 PM
|
#5
|
Member
Registered: Oct 2006
Distribution: usually use arch
Posts: 103
Original Poster
Rep:
|
yeah its now interpreting the backquoted section literally and breaking on each of the characters inside the quotes.
|
|
|
09-11-2007, 07:20 PM
|
#6
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
Quote:
Originally Posted by farkus888
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
|
|
|
09-11-2007, 07:24 PM
|
#7
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
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.
|
|
|
09-11-2007, 07:46 PM
|
#8
|
Member
Registered: Oct 2006
Distribution: usually use arch
Posts: 103
Original Poster
Rep:
|
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.
|
|
|
09-12-2007, 02:30 AM
|
#9
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by osor
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
|
|
|
All times are GMT -5. The time now is 05:56 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|