Ask a Question related to SCO, Design and Development.
-
Doc #1
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
-
variable to a shell-script
hello, i've got really problems transporting a form-variable (called searchstring) to a shellscript :-/ because of a blank in the searchstring... -
freelance web programming, web site design,c programming, java programming, VERY Low Cost web design and more
Find expert freelance programmers and designers at the prices you want to pay. Post your projects and programmers will place bids, you choose the... -
Shell Programming
I am new to UNIX and I want to get a feel for shell scripting. I have seen examples and it looks complicated! I want to know is there a good book... -
Is there a *nix shell variable for the path to perl?
Is there a (BA)SH/CSH/TSH/KSH variable to the Perl path? I am creating a script that uses Perl I want to be able to share over several servers. ... -
Shell programming question...urgent help please!
Bob C. Little <bclasd@bellsouth.net> wrote: Here's a Perl solution: ------------------------8<--------------------------------------------... -
Bob Bailin #2
Re: Shell Programming: IFS variable
"Doc" <a@b.com> wrote in message
news:hwqYa.40450$I_3.19987@twister.nyroc.rr.com...and> 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.5Here's> 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.of> 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" insteadIFS=`echo "\n\c"`> 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?
Bob Bailin Guest
-
Doc #3
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.3605@compuserve.com> wrote in message
news:bgtcsr$m48$1@ngspool-d02.news.aol.com...newline>
> "Doc" <a@b.com> wrote in message
> news:hwqYa.40450$I_3.19987@twister.nyroc.rr.com...> and> > 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> > have the following problem:
> >
> > I'm trying to set the IFS (Internal Field Seperator) variable to ait> Here's> > character so I can properly loop thru each line of a command output.> of> > 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> > 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>> > to "\" and "n".
> >
> > Anyone know how to set IFS to a true newline character?
> IFS=`echo "\n\c"`
>
>
Doc Guest
-
Ronald J Marchand #4
Re: Shell Programming: IFS variable
"Doc" <a@b.com> wrote in message
news:vXqYa.40452$I_3.8458@twister.nyroc.rr.com...the> 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" becauseTop posting is frowned on here.> \n and \c are being treated literally by the shell.
>
> Any other suggestions?
>
> - Don -
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
-
Ronald J Marchand #5
Re: Shell Programming: IFS variable
"Doc" <a@b.com> wrote in message
news:vXqYa.40452$I_3.8458@twister.nyroc.rr.com...the> 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" becauseCorrection ....> \n and \c are being treated literally by the shell.
>
> Any other suggestions?
>
> - Don -
# IFS=`echo "\n\c"`
# echo "$IFS" | hd
0000 0a .
0001
ron
Ronald J Marchand Guest
-
Doc #6
Re: Shell Programming: IFS variable
"Doc" <a@b.com> wrote in message
news:hwqYa.40450$I_3.19987@twister.nyroc.rr.com...and> 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.5Here's> 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.of> 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" insteadI figured out the fix to my problem. The solution is to use CTRL-M and> 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 -
>
>
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
-
scriptOmatic #7
Re: Shell Programming: IFS variable
Doc wrote:
IFS='>
> 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 -
'; # 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
-
Bob Bailin #8
Re: Shell Programming: IFS variable
"Doc" <a@b.com> wrote in message
news:vXqYa.40452$I_3.8458@twister.nyroc.rr.com...can't> "Bob Bailin" <72027.3605@compuserve.com> wrote in message
> news:bgtcsr$m48$1@ngspool-d02.news.aol.com...> >
> > "Doc" <a@b.com> wrote in message
> > news:hwqYa.40450$I_3.19987@twister.nyroc.rr.com...> > > Hi all,
> > >
> > > I'm having a strange little problem that MUST have an answer but I5.0.5> > > seem to figure it out! I'm using a Bourne shell on SCO Openserverinstead> newline> > and> > > have the following problem:
> > >
> > > I'm trying to set the IFS (Internal Field Seperator) variable to a> > Here's> > > character so I can properly loop thru each line of a command output.> > > 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"actually> > of> > > each newline. It seems evident that my problem is that I'm notsetting> > > setting IFS to a newline character as I'd like. But am insteadthe> it>> >> > > to "\" and "n".
> > >
> > > Anyone know how to set IFS to a true newline character?
> > IFS=`echo "\n\c"`
> >
> >> found that NOTHING ends up in IFS when I use that syntax (IFS=`echo> > Thanks for trying, but this did not work. I had tried this earlier and
> "\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" becauseNot obvious at first, but simpler than I thought:> \n and \c are being treated literally by the shell.
>
> Any other suggestions?
>
> - Don -
>
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
-
Bela Lubkin #9
Re: Shell Programming: IFS variable
Doc wrote:
Others have showed you:> 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='
' # 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_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<Bela Lubkin Guest
-
John DuBois #10
Re: Shell Programming: IFS variable
In article <wosYa.40475$I_3.23302@twister.nyroc.rr.com>, Doc <a@b.com> wrote:
Control-M produces the same effect as hitting the return key. Whether you hit>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.
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]spcecdt@armory.com[/email] KC6QKZ/AE [url]http://www.armory.com/~spcecdt/[/url]
John DuBois Guest



Reply With Quote

