Results 1 to 4 of 4

Thread: IFS bug?

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Beans
    141
    Distro
    Ubuntu 9.10 Karmic Koala

    IFS bug?

    Hello,

    I have a script that seems to have a glitch with IFS.

    If I run the script as a normal user, I get:

    Code:
    -rw-r--r-- root root /var/www/Shared/contacts.html
    -rw-r--r-- root root /var/www/Shared/index.html
    -rw-r--r-- root root /var/www/Shared/layout.css
    -rw-r--r-- root root /var/www/Shared/settings.js
    But if I run the script using sudo, I get:

    Code:
    -rw-r--r-- root root /var/www/Shared/co
    tacts.html
    -rw-r--r-- root root /var/www/Shared/i
    dex.html
    -rw-r--r-- root root /var/www/Shared/layout.css
    -rw-r--r-- root root /var/www/Shared/setti
    gs.js
    And here is the script:

    Code:
    oIFS="$IFS"
    IFS=$'\n'
    for line in `cat 'old_permissions'`; do
    	echo $line
    done
    It seems that the "n's" in the filenames are becoming linebreaks... Is this a glitch or is my code wrong?

    Thanks,

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: IFS bug?

    add hashbang
    Code:
    #!/bin/bash
    probably root doesn't use bash and the shell used doesn't recognize the $'...' construct, so the separators are $ and n

    besides use while to iterate over files and command outputs, it eliminates the need for touching IFS altogether

    Code:
    while read -r line; do
      echo $line
    done < file
    
    while read -r line; do
      echo $line
    done < <( command )
    Last edited by Vaphell; July 11th, 2012 at 10:34 PM.

  3. #3
    Join Date
    Mar 2010
    Beans
    141
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: IFS bug?

    Yay that fixed it!

  4. #4
    Join Date
    Jul 2007
    Location
    Burlington, NC
    Beans
    1,995
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: IFS bug?

    Mangling the IFS introduces a whole class of bugs into shell scripts that can and should be easily avoided...

    Code:
    #wrong, even with IFS hacks...
    for file in `ls /some/dir`; do ...
    
    #right, bug free, no IFS hacks needed...
    for file in /some/dir/*; do ...
    In your case:

    Code:
    #naughty IFS hacks...
    oIFS="$IFS"
    IFS=$'\n'
    for line in `cat 'old_permissions'`; do
    	echo $line
    done
    
    #not necessary ...
    cat old_permissions | while read line
    do
        echo "$line" #note below
    done
    Also note the quotation marks. At first glance in the following you may think that read is eating whitespace which is undesirable. But it's actually caused by the lack of quotes with the echo statement.
    Code:
    $ read line
    test   123
    $ echo $line
    test 123
    $ echo "$line"
    test   123
    Last edited by asmoore82; July 12th, 2012 at 02:53 PM.
    Give me Free Software or Give me Death!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •