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:


Auto FTP Manager



  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<





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.




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



LOD Communications, Inc.

/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


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!





More:
       - Bela








card_image







Change Congress