Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Shell Programming: IFS variable

瀏覽次數:75 次
跳到第一則未讀訊息

Doc

未讀,
2003年8月7日 清晨7:14:212003/8/7
收件者:
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 -


Bob Bailin

未讀,
2003年8月7日 清晨7:23:292003/8/7
收件者:

"Doc" <a...@b.com> wrote in message
news:hwqYa.40450$I_3....@twister.nyroc.rr.com...

IFS=`echo "\n\c"`


Doc

未讀,
2003年8月7日 清晨7:43:232003/8/7
收件者:
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...@compuserve.com> wrote in message
news:bgtcsr$m48$1...@ngspool-d02.news.aol.com...

Ronald J Marchand

未讀,
2003年8月7日 上午8:24:542003/8/7
收件者:
"Doc" <a...@b.com> wrote in message
news:vXqYa.40452$I_3....@twister.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

未讀,
2003年8月7日 上午8:45:552003/8/7
收件者:
"Doc" <a...@b.com> wrote in message
news:vXqYa.40452$I_3....@twister.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

Doc

未讀,
2003年8月7日 上午9:21:262003/8/7
收件者:
"Ronald J Marchand" <roj...@covad.net> wrote in message
news:bgthfr$ig9$1...@sun-news.laserlink.net...

My sincere apologies for "top-posting".

- Don -


Doc

未讀,
2003年8月7日 上午9:22:362003/8/7
收件者:
"Doc" <a...@b.com> wrote in message
news:hwqYa.40450$I_3....@twister.nyroc.rr.com...

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 -


scriptOmatic

未讀,
2003年8月7日 上午10:40:302003/8/7
收件者:

IFS='
'; # set the IFS to a NEWLINE

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

Bob Bailin

未讀,
2003年8月7日 下午4:51:202003/8/7
收件者:

"Doc" <a...@b.com> wrote in message
news:vXqYa.40452$I_3....@twister.nyroc.rr.com...

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


Bela Lubkin

未讀,
2003年8月7日 下午6:11:292003/8/7
收件者:sco...@xenitec.ca
Doc wrote:

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_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<

John DuBois

未讀,
2003年8月13日 凌晨12:22:292003/8/13
收件者:
In article <wosYa.40475$I_3....@twister.nyroc.rr.com>, Doc <a...@b.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 spc...@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/

0 則新訊息