Why IFS did not effect?



  • 1. Wipe all environment variables
    Does anyone know a neat way to unset all environment variables from the current shell? It would be nice if the "Shell Variables" as listed in the bash manpage were unaffected. Thanks, David
  • 2. Extracting lines from a text file that match a certain criteria to another text file
    Hi, I am novice UNIX shell programmer and would like some help. I am trying to write a shell script that will read a text file line by line. The task is to extract lines from the text file that meet a set of criteria to another text file. An example of the text file, foo2, that I am using is shown below, 0,0,"NBSC",12,"24/02/2004 15:35:34","RD","2.2" 1,1,"ORG-UNIT-TYPE","C",1,"13/03/1997 17:24:49","PO" 1,2,"ORG-UNIT-TYPE","C",2,"13/03/1997 17:24:49","Client" 1,3,"ORG-UNIT-HIST","C",3,"13/03/1997 17:24:49","PON" The task is to look at the 3rd field of each line, and extract the whole line if the third field matches the criteria. The criteria is to extract all lines that have the 3rd field set to the value of "NBSC" to a separate text file. Similarly to extract all lines, to another separate file, that have the 3rd field set to the value of "ORG-UNIT-HIST". All lines with 3rd field set to, "ORG-UNIT-TYPE" are to be ignored. The psuedo code that I have come up with so far is shown below, while read -r rec_type do NEXT=`echo ${rec_type} | cut -f 3 -d,` if "${NEXT}" = "NBSC" then write to text file1 else if "${NEXT}" = "ORG-UNIT-HIST" then write to text file 2 fi fi done < foo2 Any help or pointers would be greatly appreciated. Thanks in antcipation
  • 3. find newest files in directories and subdirs (recursive)
    Hello, I need to find the newest files (mtime) within a directory _and_ all sub directories. On a Linix box I can e.g. do ll -RA --full-time *|cut -c44- |sort |tail -10 this gives me the 10 oldest files. I still don't know the path the files are in, but better than nothing. any suggestions? Rgds, Alex
  • 4. very simple question
    in a simple command sequance such as: 1. cd !*;pwd 2. rm -i !* what does the characters "!*" do? do they carry the special meanings in this case? thanks alot.

Why IFS did not effect?

Postby Bo Yang » Fri, 08 May 2009 12:37:19 GMT

   I write following scripts:

    IFS=",";set | grep "IFS"; for name in 0.01,0.02; do echo "$name";

   And its ouput is   "0.01,0.02", But I think it should be :

  The IFS variable take no effect. Could you please tell me why?


Re: Why IFS did not effect?

Postby Barry Margolin » Fri, 08 May 2009 14:27:04 GMT

In article 

IFS is only used after variable expansion, not when parsing the original 
command line.  Otherwise, you would have to write:


Barry Margolin,  XXXX@XXXXX.COM 
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Re: Why IFS did not effect?

Postby Marco Maggi » Fri, 08 May 2009 17:27:12 GMT

You need the following:

for name in $stuff
do echo "$name"
printf 'inner [%s]\n' "$IFS"
printf 'after [%s]\n' "$IFS"

notice that,  at least under Bash,  "for" is a  syntax so we
have to  save IFS and  restore it later; the  following will
raise an error "for: command not found":

IFS=, for name in $stuff
do echo "$name"

Marco Maggi

Re: Why IFS did not effect?

Postby Wayne » Mon, 11 May 2009 23:05:40 GMT

This is true but makes no sense to me; IFS is used for
word splitting which occurs after expansions.  Token
recognition occurs first and uses blanks to delimit
tokens (it is in reality more complex that this, but
IFS is definitely NOT consulted at this point.)

So with a for loop (compound command), reserved words
should be recognized.  According to the man pages
(and SUS4, Sec. 2.9.4) only the list of words following
the "in" keyword are expanded, and thus subject to IFS,
at least as far as I understand it.

This behavior occurs in ksh too (and still doesn't
work in zsh but that give a different error).

This works:
IFS=, eval 'for i in $stuff; do printf "%s\n"  $i; done'

and you can of course use:
( IFS=,; for i in $stuff; do printf '%s\n' "$i"; done )

But why should you get errors with these:
   $ IFS=, for i in $FOO; do echo "$i"; done;
   -bash: syntax error near unexpected token `do'
   $ IFS=, for i in $FOO
   -bash: for: command not found
(And similarly for ksh.)

What am I missing?


Re: Why IFS did not effect?

Postby pk » Tue, 12 May 2009 05:27:55 GMT

Variable assignment cannot precede a reserved word. What you noted is true
not only for the "for" reserved word, but also for the others:

$ foobar=10 if true; then echo foo; fi
bash: syntax error near unexpected token `then'

$ foobar=10 while true; do echo foo; done
bash: syntax error near unexpected token `do'


According to posix, reserved words are:

!     do     esac   in
{     done   fi     then
}     elif   for    until
case  else   if     while

and reserved words are recognized as follows:

"This recognition shall only occur when none of the characters is quoted and
when the word is used as:

    * The first word of a command

    * The first word following one of the reserved words other than case,
for, or in
    * The third word in a case command (only in is valid in this case)
    * The third word in a for command (only in and do are valid in this

so if a variable assignment is at the beginning of the line, the reserved
word is not recognized.
BTW, also not that they must not be quoted:

$ "for" i in a; do echo $i; done
bash: syntax error near unexpected token `do'

Maybe you want this:

for i in \; ; do echo $i; done

That is just normal syntax if you want to write the for command all on a
single line:

for i in $FOO; do echo $i; done

(module the variable assignment at the beginning which is not valid before a
reserved word)

Same as before.

Similar Threads:

1.Why #pragma pack() not take effect?

Struct definition as following(on 32-bit Linux):

#pragma pack(push, 8)
struct MY_STRUCT
    char a[2];
    short b;
    short c;
    short d;
    int e;
    long long x;
    long long y;

During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
two. Ain't I right?

If I get wrong usage of #pragma pack(), could anyone please tell me how to
get it work correctly?

BTW, what on earth is the difference between __attribute__ align() and
#pragma pack()?

Thanks in advance!

2.What does IFS="${IFS}[]()" mean?

I have a ksh script as below.
jobs -l | while IFS="${IFS}[]()" read ...

I know IFS is for Field Separator;
However I can't understand what does IFS="${IFS}[]()" mean???

Can Anybody help me on above?


3.differecnce between ${IFS} and "${IFS}"

The following two commands have different effect.

$ echo A${IFS}B

$ echo "A${IFS}B"

It is obvious that the first one interprets `IFS' as
space, while the second interprets it as new line.
In fect, the `IFS' represents three characters, namely
space, tab, and new line. why different choices occur 
in different situations? 

ego cogito ergo sum 

4.Apache 1.3 [unix]: Directives not having effect


I've been experimenting with Apache for the past few days.  I've made
a few changes but they aren't having the desired effect.

1. When entering the site, the user automatically sees index.htm page.
   My Sol: DirectoryIndex index.htm

2. A page in a sub dir is blocked to everybody except a particular IP.
   <Directory /home/rpandyan/apache/htdocs/block>
      Order Deny,Allow
      Deny from all
      Allow james.virtual.com

3. I have password protected a few pages also but that also isn't
   <Directoy /home/rpandyan/apache/htdocs/hidden>
      AuthType Basic
      AuthName "Restricted Files"
      AuthUserFile /home/rpandyan/apache/passwd/passwords
      Require user James

Note: I have made all changes to the httpd.conf file for efficienty
      The Listen directive works [I see the test page]

If anybody knows what I need to do, please let me know.

5.Why isn't more work done with winelib?


Had this experience a couple of times.

I've really wanted some software package, but there isn't a binary
made specially for my distro. I've tried installing from source, only to
discover that it desperately needs some dependency but it won't tell me
what it is, and the srpm just won't work for various reasons.

So I've downloaded the Windows version and discovered it works adequately
with wine.

Why isn't more work done with winelib, rather than win32 - at least for
desktop programs? Rather than making a separate Windows, and Linux
version, you could kill two birds with one stone. 

Also makes installing software relatively easy and painless.

6. Why isn't anyone doing Marketing for FOSS ??

7. Why am I doing this???

8. Why Desktop Linux installs in 9 minutes and what can be done to distribute it widely

Return to unix


Who is online

Users browsing this forum: No registered users and 55 guest