Question regarding IFS and set -- : Why does it take two iterations?

Question regarding IFS and set -- : Why does it take two iterations?

Post by Jeremy Mathe » Mon, 02 Mar 1998 04:00:00



I am using IFS to parse out a line in sh (actually, bash under Linux).

I have something like:

        foo=`IFS=/;set -- /foo/bar/bletch/some/filename;echo $5`

However, it doesn't work.  After the first 'set', everything is in $1, not
in the other args (although it does convert the slashes into spaces).
To get what I really want, I have to do:

        foo=`IFS=/;set -- /foo/bar/bletch/some/filename;set -- $1;echo $5`

This correctly gets me 'some' (Note that $1 is null).

But why do I need the additional 'set' command?  Is this WAD (*) ?
It seems to me that the first form ought to work.

(*) Working as designed.

************************************************************************
Are you joking?  DOS applications in WinNT?  WinNT is just a toy
you install on your machine, to give more shine to OS/2's glory.
                - Mikael Wahlgren -


          hundreds, if not thousands, of dollars, every time he posts -
************************************************************************
rwvpf wpnrrj ibf ijrfer

 
 
 

Question regarding IFS and set -- : Why does it take two iterations?

Post by Barry Margoli » Mon, 02 Mar 1998 04:00:00




>I have something like:

>    foo=`IFS=/;set -- /foo/bar/bletch/some/filename;echo $5`

>However, it doesn't work.  After the first 'set', everything is in $1, not
>in the other args (although it does convert the slashes into spaces).

It works for me, in /usr/bin/sh under Solaris 2.5.1.  

$ foo=`IFS=/;set -- /foo/bar/bletch/some/filename;echo $5`
$ echo $foo

--

GTE Internetworking, Powered by BBN, Cambridge, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.

 
 
 

Question regarding IFS and set -- : Why does it take two iterations?

Post by Jeremy Mathe » Tue, 03 Mar 1998 04:00:00




...

Quote:>It works for me, in /usr/bin/sh under Solaris 2.5.1.  

>$ foo=`IFS=/;set -- /foo/bar/bletch/some/filename;echo $5`
>$ echo $foo

Yup, you're right.  It is a bash problem.  I just tested it on this Solaris
system (which has both sh and bash), and sh got it right; bash got it wrong.

Can there be a sense in which this is a feature, or does it just have to
considered a bash bug?  Anyway, I guess on Linux, I'm stuck.

************************************************************************

                        The Official JM Personality Test

Part I. Answer these questions:

        1) You are visiting a friend in his office and you notice two
           calendars on adjacent walls.  On one wall, is a Three Stooges
           calendar; on the other, one of the Sports Illustrated Swimsuit
           Edition.  Which one is more likely to hold your
           interest/attention?

        2) You are driving on an urban freeway (not at rush hour) and
           there is a traffic snarl, that looks like it could go on for
           some time.  As you crawl forward, you notice an exit to a
           surface street that looks like it might get you where you
           are going more quickly than the freeway will.  Are you
           likely to take the exit or would you just stay where you
           are and duke it out with everybody else?

        3) You go into a restaurant and order a Rueben sandwhich.
           When it arrives, you are not pleased with its
           price/performance ratio.  Are you more likely to say that
           it is not big enough or that it is too expensive?

Part II. Now, write an essay for each one telling what you think it
        says about you/What the purpose of each question is.


          hundreds, if not thousands, of dollars, every time he posts -
************************************************************************
rwvpf wpnrrj ibf ijrfer

 
 
 

Question regarding IFS and set -- : Why does it take two iterations?

Post by Andreas Schwa » Tue, 03 Mar 1998 04:00:00




|> ...
|>> It works for me, in /usr/bin/sh under Solaris 2.5.1.  
|>>
|>> $ foo=`IFS=/;set -- /foo/bar/bletch/some/filename;echo $5`
|>> $ echo $foo

|> Yup, you're right.  It is a bash problem.  I just tested it on this Solaris
|> system (which has both sh and bash), and sh got it right; bash got it wrong.

Actually bash gets it right.

|> Can there be a sense in which this is a feature, or does it just have to
|> considered a bash bug?  Anyway, I guess on Linux, I'm stuck.

It's not a bug, it's a security feature.  Bash does IFS splitting only on
the result of expansions.  Use the following:

$ foo=`IFS=/;set -- /foo/bar/bletch/some/filename$empty;echo $5`

--
Andreas Schwab                                      "And now for something


 
 
 

Question regarding IFS and set -- : Why does it take two iterations?

Post by Jeremy Mathe » Tue, 03 Mar 1998 04:00:00




...

Quote:>Actually bash gets it right.

>|> Can there be a sense in which this is a feature, or does it just have to
>|> considered a bash bug?  Anyway, I guess on Linux, I'm stuck.

>It's not a bug, it's a security feature.  Bash does IFS splitting only on
>the result of expansions.  Use the following:

>$ foo=`IFS=/;set -- /foo/bar/bletch/some/filename$empty;echo $5`

Hmmm.  Yes, that works.  One kludge or another...

I had the feeling that there was some reason why somebody would consider it
a feature - that is why I asked the question above in the way I did.
I am familiar with the security implications of IFS.

Note, BTW, that my actual example in practice is something like:

        /foo/bar/bletch/some/file*

where I am doing filename expansion - yet in that case, it doesn't do the
IFS thing.  So, it is not just any expansion...

************************************************************************
        "Tucked away in winter quarters,
        Gainsborough's sons and Buchan's daughters,
        Blue of *, clean-lined, and handsome,
        Priced beyond a prince's ransom,
        Where no danger can befall them,
        Rest till next year's Classics call them."

                        - Will H Ogilvie -


          hundreds, if not thousands, of dollars, every time he posts -
************************************************************************
rwvpf wpnrrj ibf ijrfer

 
 
 

Question regarding IFS and set -- : Why does it take two iterations?

Post by Veksler Micha » Tue, 03 Mar 1998 04:00:00


[cut]

Quote:>  |> Can there be a sense in which this is a feature, or does it just have to
>  |> considered a bash bug?  Anyway, I guess on Linux, I'm stuck.
>  It's not a bug, it's a security feature.  Bash does IFS splitting only on
>  the result of expansions.  Use the following:
>  $ foo=`IFS=/;set -- /foo/bar/bletch/some/filename$empty;echo $5`

Can someone explain me, what is the security risk?
I looked at the FAQ, and found that IFS is considered a security risk,
just as PATH is. (PATH and SHELL I can understand, but IFS !?).

>  --
>  Andreas Schwab                                      "And now for something



Thanks
  Michael
 
 
 

Question regarding IFS and set -- : Why does it take two iterations?

Post by Allen Kirb » Tue, 03 Mar 1998 04:00:00


cut -d/ -f5 avoids the problem altogether, although it does spawn a process.


> >It's not a bug, it's a security feature.  Bash does IFS splitting only on
> >the result of expansions.  Use the following:

> >$ foo=`IFS=/;set -- /foo/bar/bletch/some/filename$empty;echo $5`

> Hmmm.  Yes, that works.  One kludge or another...

> <snip>

--
Allen Kirby
AT&T Information Technology Services
Alpharetta, GA.
The views expressed here are mine, not my employers.
 
 
 

Question regarding IFS and set -- : Why does it take two iterations?

Post by Christian Weisgerb » Tue, 03 Mar 1998 04:00:00




> I am using IFS to parse out a line in sh (actually, bash under Linux).
> I have something like:

>    foo=`IFS=/;set -- /foo/bar/bletch/some/filename;echo $5`

> However, it doesn't work. After the first 'set', everything is in $1, not

I don't see why it should.
Field splitting is performed on substitution results, not on constant
strings.
bash, pdksh, and FreeBSD sh agree on this. (The O'Reilly Korn Shell
book, too.)

--

  See another pointless homepage at <URL:http://home.pages.de/~naddy/>.

 
 
 

Question regarding IFS and set -- : Why does it take two iterations?

Post by brian hil » Sat, 07 Mar 1998 04:00:00


: Can someone explain me, what is the security risk?
: I looked at the FAQ, and found that IFS is considered a security risk,
: just as PATH is. (PATH and SHELL I can understand, but IFS !?).

in [original non-POSIX] bourne shell, if IFS was set to (let's say...)
"x", any script spawned with this environment would, when encountering
an "exit" command, actually execute command "e" with argument "it".
Now, if one were to _write_ such a script "e", then it would execute
under the privileges of the owner of the original script! Bad news.

Kornshell (and other POSIX-compliant shells) will avoid this by
mandating an initial value of <space><tab> for IFS regardless of
the exported value of IFS in the environment and only using the
value of IFS to do word-separation upon variable substitution and
read inputs.

-Brian

 
 
 

Question regarding IFS and set -- : Why does it take two iterations?

Post by Heiner Steve » Mon, 09 Mar 1998 04:00:00



> I am using IFS to parse out a line in sh (actually, bash under Linux).

> I have something like:

>         foo=`IFS=/;set -- /foo/bar/bletch/some/filename;echo $5`

Could it be the case you are simulating the "basename" command?

Heiner
--
  -------------------------------------------------------------

/ Milky Way, Solar System, Earth, Europe, Bavaria, Nuernberg /
-------------------------------------------------------------

 
 
 

Question regarding IFS and set -- : Why does it take two iterations?

Post by Jeremy Mathe » Tue, 10 Mar 1998 04:00:00





>> I am using IFS to parse out a line in sh (actually, bash under Linux).

>> I have something like:

>>         foo=`IFS=/;set -- /foo/bar/bletch/some/filename;echo $5`

>Could it be the case you are simulating the "basename" command?

Good point, although it turns out that in the actual application, the
filename component contains wildcards, and I need to pick off the last file
that matches.

As others have pointed out, there are certainly a million ways to do this;
one of the attractions of using IFS is that no processes are forked.

I ended up with:

 foo=`IFS=/;set -- /foo/bar/bletch/some/file*;eval set \\$$#;eval echo \\$$#`

which works nicely (although I typed that in from memory, so there may
be typos).  Once one get used to it, one sees that the actual behavior of
the shell is the desirable behavior.

************************************************************************
Pure balls. (David Spade)


          hundreds, if not thousands, of dollars, every time he posts -
************************************************************************
rwvpf wpnrrj ibf ijrfer

 
 
 

1. Problem on tcp socket:two consecutive writes takes much much longer time than single write, why?

Hi, guys,

I met a weird problem with tcp socket programming on linux
and I appreciate your patience to read this. I really need someone
to help me out.

I have two PCs (dual-AMD Athlon 1.4G CPU) running RH7.2. Both on
a local ethernet network.

The problems can be simplied and summarized as following:

Program Version 1:

     (PC1)
     ...
     listen();
     accept();
-->
W1   Write(PC2, 100 bytes);
-->
R1   Read(PC2, 100 bytes);
-->
     end.

    (PC2)
    ...
    connect();
-->
R1  Read(PC1, 100 bytes);
-->
W1  Write(PC1, 100 bytes);
-->
    end.

A TCP connection is setup at the beginning of the program,
and timers are added at "-->". So W1 and R1 are the time spent
on Write and Read. (Read and Write are actually wrapper
functions for read and write.)
I checked the times after the program terminated.
Everything was just fine. Then I came to version 2.

Program Version 2:

    (PC1)
    ...
    listen();
    accept();
-->
W1  Write(PC2, 50 bytes);
-->
W2  Write(PC2, 50 bytes);
-->
R1  Read(PC2, 100 bytes);
-->
  end.

    (PC2)

    ...
    connect();
-->
R1  Read(PC1, 100 bytes);
-->
W1  Write(PC1, 50 bytes);
-->
W2  Write(PC1, 50 bytes);
-->
  end.

I found total 100 bytes from pc1 to pc2 took a very very long time.
Almost 100 times long!

See my data:

Version 1:
   PC1:   W1 = 32us,   R1=239us
   PC2:   R1 = 413us,  W1=18us

Version 2:    
   PC1:   W1 = 12us,     W2=2us,  R1=42812us
   PC2:   R1 = 42846us,  W1=8us,  W2=2us

Does anybody know why?

Daniel

2. E2FSCK on kernel startup.

3. Question regarding jumpstart server having two interfaces

4. Q:Creating Diff script to ADD file1 stuff to file2

5. Networking question regarding two offices seperated by T1

6. Strange KDE behaviour

7. Question regarding comparing two files.

8. : ir247_config-2.diff

9. two questions regarding netscape navigator

10. Two questions: One regarding kernel messages with IP Masquerade and one on Redhat 4.0 startup

11. lilo -> DOS WD 32 bit IFS??

12. Question regarding setting user's environment after login

13. Bootp and two network IFs