If this isn't exactly what you wanted, please try our Search (there's a LOT of techy and non-techy stuff here about Linux, Unix, Mac OS X and just computers in general!):
From: Bela Lubkin <belal@sco.com> Subject: Re: Shell Programming: IFS variable Date: Thu, 7 Aug 2003 22:11:29 GMT References: <hwqYa.40450$I_3.19987@twister.nyroc.rr.com> Doc wrote: > I'm having a strange little problem that MUST have an answer but I can't > seem to figure it out! I'm using a Bourne shell on SCO Openserver 5.0.5 and > have the following problem: > > I'm trying to set the IFS (Internal Field Seperator) variable to a newline > character so I can properly loop thru each line of a command output. Here's > a snip of my code: > IFS='\n' > dex=0 > for nxt in `ps` > do > dex=`expr $dex + 1` > e=`echo $nxt|cut -f1 -d' '` > eval jbid_$dex=$e > echo "$dex) $nxt" > done > > The problem is that the shell is seperating my lines at each "n" instead of > each newline. It seems evident that my problem is that I'm not actually > setting IFS to a newline character as I'd like. But am instead setting it > to "\" and "n". > > Anyone know how to set IFS to a true newline character?
Others have showed you:
IFS='
' # actual newline in quotes
My usual idiom for what you're doing is:
ps | while read nxt; do
...
done
But there's a gotcha here. The Bourne shell forks a separate shell for
a set of shell commands that are being piped to. It does a decent job
of hiding this fact, but your:
eval jbid_$dex=$e
statements won't come through. The variables $jbid_xxx will get set in the sub-shell, but they won't exist by the time you come to try to use them, back in the parent shell. The Korn shell does not fork a separate shell in this situation; running the same script under `ksh` would make it work. It was a design goal of ksh to eliminate these language oddities where the scope of a variable is constrained by forces outside your control. >Bela<
/Bofcusm/2273.html copyright 1997-2004 (various authors) All Rights Reserved
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar