Best of the Newsgroups: sh vs. ksh


What is this stuff?

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>


Hate these ads?



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:


ad



  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<







Comments /Bofcusm/2273.html


Add your comments

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner





Views for this page
Today This Week This Month This Year  Overall
730201,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.

Publishing your articles here

More:
       - Bela




Unix/Linux Consultants

Your ad here - $24.00 yearly!

http://www.vss3.com SCO/Caldera OpenServer, Unixware & Linux. Tarantella & Non-stop Clustering


http://echo3.net/ Unix/Linux Custom Applications, Web Hosting, C/C++ Programming Courses


http://www.breakthru.com.au SCO (Openserver and Unixware), Unix, Solaris and Linux Consulting services including: Secure Networking Solutions; Linux based Firewalls; Backup Solutions; Secure Home to Office Network Setup; Phone, Remote and On-Site Support available - Satisfaction Guaranteed!




Twitter
  • Jun 26 21:28
    Lost 5 cents at poker tonight. Hard to do with 10 and 20 cent betting..
  • Jun 23 07:01
    Hypermiling:




card_image








Change Congress

Related Posts