Professional Web Applications Themes

Shell Programming: IFS variable - SCO

Hi all, 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 ...

  1. #1

    Default Shell Programming: IFS variable

    Hi all,

    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?

    Thanks,

    - Don -


    Doc Guest

  2. #2

    Default Re: Shell Programming: IFS variable


    "Doc" <ab.com> wrote in message
    news:hwqYa.40450$I_3.19987twister.nyroc.rr.com...
    > Hi all,
    >
    > 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?
    IFS=`echo "\n\c"`


    Bob Bailin Guest

  3. #3

    Default Re: Shell Programming: IFS variable

    Thanks for trying, but this did not work. I had tried this earlier and
    found that NOTHING ends up in IFS when I use that syntax (IFS=`echo
    "\n\c"`). And if I try setting it directly (IFS="\n\c"), I'm back to the
    same problem. My lines now get seperated on any "n" or any "c" because the
    \n and \c are being treated literally by the shell.

    Any other suggestions?

    - Don -

    "Bob Bailin" <72027.3605compuserve.com> wrote in message
    news:bgtcsr$m48$1ngspool-d02.news.aol.com...
    >
    > "Doc" <ab.com> wrote in message
    > news:hwqYa.40450$I_3.19987twister.nyroc.rr.com...
    > > Hi all,
    > >
    > > 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?
    >
    > IFS=`echo "\n\c"`
    >
    >

    Doc Guest

  4. #4

    Default Re: Shell Programming: IFS variable

    "Doc" <ab.com> wrote in message
    news:vXqYa.40452$I_3.8458twister.nyroc.rr.com...
    > Thanks for trying, but this did not work. I had tried this earlier and
    > found that NOTHING ends up in IFS when I use that syntax (IFS=`echo
    > "\n\c"`). And if I try setting it directly (IFS="\n\c"), I'm back to the
    > same problem. My lines now get seperated on any "n" or any "c" because
    the
    > \n and \c are being treated literally by the shell.
    >
    > Any other suggestions?
    >
    > - Don -
    Top posting is frowned on here.

    My default IFS is set to hex 0a in the bourne shell.

    Try echo $IFS | hd and see what you get.

    Ron



    Ronald J Marchand Guest

  5. #5

    Default Re: Shell Programming: IFS variable

    "Doc" <ab.com> wrote in message
    news:vXqYa.40452$I_3.8458twister.nyroc.rr.com...
    > Thanks for trying, but this did not work. I had tried this earlier and
    > found that NOTHING ends up in IFS when I use that syntax (IFS=`echo
    > "\n\c"`). And if I try setting it directly (IFS="\n\c"), I'm back to the
    > same problem. My lines now get seperated on any "n" or any "c" because
    the
    > \n and \c are being treated literally by the shell.
    >
    > Any other suggestions?
    >
    > - Don -
    Correction ....
    # IFS=`echo "\n\c"`
    # echo "$IFS" | hd
    0000 0a .
    0001

    ron



    Ronald J Marchand Guest

  6. #6

    Default Re: Shell Programming: IFS variable

    "Doc" <ab.com> wrote in message
    news:hwqYa.40450$I_3.19987twister.nyroc.rr.com...
    > Hi all,
    >
    > 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?
    >
    > Thanks,
    >
    > - Don -
    >
    >
    I figured out the fix to my problem. The solution is to use CTRL-M and
    quotes. In my shell script, setting IFS now looks like this:
    IFS="
    "
    The second quote is on the next line because I hit "Control-M" after the
    first quote.

    Thanks to all for thinking about it for me.

    - Don -


    Doc Guest

  7. #7

    Default Re: Shell Programming: IFS variable

    Doc wrote:
    >
    > Hi all,
    >
    > 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?
    >
    > Thanks,
    >
    > - Don -
    IFS='
    '; # set the IFS to a NEWLINE

    --
    [url]http://ftp.opensysmon.com[/url] is a shell script archive site with an
    open source system monitoring and network monitoring software package.
    Many platforms are supplied already compiled.
    scriptOmatic Guest

  8. #8

    Default Re: Shell Programming: IFS variable


    "Doc" <ab.com> wrote in message
    news:vXqYa.40452$I_3.8458twister.nyroc.rr.com...
    > "Bob Bailin" <72027.3605compuserve.com> wrote in message
    > news:bgtcsr$m48$1ngspool-d02.news.aol.com...
    > >
    > > "Doc" <ab.com> wrote in message
    > > news:hwqYa.40450$I_3.19987twister.nyroc.rr.com...
    > > > Hi all,
    > > >
    > > > 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?
    > >
    > > IFS=`echo "\n\c"`
    > >
    > >
    >
    > > Thanks for trying, but this did not work. I had tried this earlier and
    > found that NOTHING ends up in IFS when I use that syntax (IFS=`echo
    > "\n\c"`). And if I try setting it directly (IFS="\n\c"), I'm back to the
    > same problem. My lines now get seperated on any "n" or any "c" because
    the
    > \n and \c are being treated literally by the shell.
    >
    > Any other suggestions?
    >
    > - Don -
    >
    Not obvious at first, but simpler than I thought:

    IFS='
    '

    What this means is, type: IFS=<single quote><enter key><single quote><enter
    key>
    After you type the 1st enter key, you'll get your shell prompt to continue
    entering the command.
    The 1st enter key is treated as a line feed (ctrl-J), not as a termination
    character, because
    of the single quote, which starts a string that escapes almost all
    characters from
    the shell. The second single quote ends this string and the 2nd enter key
    ends the command

    $ echo "$IFS" | hd
    0000 0a 0a
    0002

    The 1st "0a" (line feed) is from the IFS variable, the 2nd is from the echo
    command.

    What you really want to see is:

    $ echo "$IFS\c" | hd
    0000 0a
    0001

    Bob


    Bob Bailin Guest

  9. #9

    Default Re: Shell Programming: IFS variable

    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:

    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_ 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<
    Bela Lubkin Guest

  10. #10

    Default Re: Shell Programming: IFS variable

    In article <wosYa.40475$I_3.23302twister.nyroc.rr.com>, Doc <ab.com> wrote:
    >I figured out the fix to my problem. The solution is to use CTRL-M and
    >quotes. In my shell script, setting IFS now looks like this:
    >IFS="
    >"
    >The second quote is on the next line because I hit "Control-M" after the
    >first quote.
    Control-M produces the same effect as hitting the return key. Whether you hit
    the return key or press control-M, the resulting ^M character is mapped to a
    newline by the line discipline (or your editor). You'd get the same effect (it
    would work) regardless of whether you typed IFS="<control-M>" or
    IFS="<return>".

    John
    --
    John DuBois [email]spcecdtarmory.com[/email] KC6QKZ/AE [url]http://www.armory.com/~spcecdt/[/url]
    John DuBois Guest

Similar Threads

  1. variable to a shell-script
    By michibeck in forum Macromedia ColdFusion
    Replies: 0
    Last Post: April 13th, 10:41 AM
  2. Replies: 0
    Last Post: May 5th, 06:41 PM
  3. Shell Programming
    By Dominick DiMantova in forum Linux / Unix Administration
    Replies: 4
    Last Post: October 23rd, 01:41 PM
  4. Is there a *nix shell variable for the path to perl?
    By Dan Anderson in forum PERL Beginners
    Replies: 1
    Last Post: September 29th, 09:24 PM
  5. Shell programming question...urgent help please!
    By Jens.Toerring@physik.fu-berlin.de in forum UNIX Programming
    Replies: 1
    Last Post: August 7th, 11:52 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139