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<
Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)
| Views for this page | ||||
|---|---|---|---|---|
| Today | This Week | This Month | This Year | Overall |
| 7 | 30 | 20 | 1,111 | 4,222 |
/Bofcusm/2273.html copyright 1997-2004 Bela Lubkin 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.

Add your comments