Re: for command

From: Harvey, Bruce T. (BTHarvey@LMUS.LEGGMASON.COM)
Date: Thu Nov 06 2003 - 11:53:16 EST


Incorrect. It is not an "O/S" issue. It is a "shell" issue. For example,
the lowest common denominator Bourne shell, this is true under AIX (try
running the following command using /bin/bsh rather than /bin/ksh ...
/bin/sh is a link to /bin/ksh). Korn handles all this so that variable is
in fact changed, but Bourne does not. I don't run csh or others, but why
take chances when you know there are variants that handle things
differently?

Example

----------------------------
tst.fil:

1
2
3
4
5

----------------------------
tst.shell:

variable="4"
cat tst.fil | while read f
do
        variable="5"
done
echo "variable == $variable"

----------------------------
When you run tst.shell under bsh, variable == 4. Under ksh, variable == 5.
Try the others and see what you get. For csh and tcsh you probably have to
change the syntax (never used) which might invalidate the test.

Bruce T. Harvey
Legg Mason Wood Walker, Inc.
Corporate Technology - UNIX Admin.
Red Run 2nd Floor - Owings Mills, MD
(410) 580-7383 - BTHarvey@LeggMason.com
-------------------------------------------------------

-----Original Message-----
From: Zebrouvis, Lois [mailto:zebrouvis@PPG.COM]
Sent: Thursday, November 06, 2003 10:38 AM
To: aix-l@Princeton.EDU
Subject: Re: for command

that is not true for aix or hp-ux - that is a linux issue

-----Original Message-----
From: Harvey, Bruce T. [mailto:BTHarvey@LMUS.LEGGMASON.COM]
Sent: Thursday, November 06, 2003 9:36 AM
To: aix-l@Princeton.EDU
Subject: Re: for command

Keep in mind that the shell creates a subshell to feed the pipe to, such
that if you change a program variable within the 'while' loop, it will not
end up changed after the loop. For example:

variable=2

cat testfile | while read f
do
        variable=3
done

when you reach here, variable is equal to 2

Just a side note to remember that I usually forget at least once every two
years or so.

Bruce T. Harvey
Legg Mason Wood Walker, Inc.
Corporate Technology - UNIX Admin.
Red Run 2nd Floor - Owings Mills, MD
(410) 580-7383 - BTHarvey@LeggMason.com
-------------------------------------------------------

-----Original Message-----
From: Paul LaMadeleine [mailto:plamadeleine@LIGHTBRIDGE.COM]
Sent: Thursday, November 06, 2003 9:22 AM
To: aix-l@Princeton.EDU
Subject: Re: for command

I usually do something like this:

cat testfile | while read line
do
   echo $line
done

At 02:10 PM 11/6/2003 +0000, you wrote:
>Given the following data how do I get the following script to echo each
>line, rather than each string?
>
>for line in `cat testfile`
>do
> echo $line
>done
>
>
>testfile =
>
>hhhh jjjjjjj kkkkkkkkk
>ggggg lllllll mmmmmm
>sssss kkkkk llllll
IMPORTANT: The security of electronic mail sent through the Internet
is not guaranteed. Legg Mason therefore recommends that you do not
send confidential information to us via electronic mail, including social
security numbers, account numbers, and personal identification numbers.

Delivery, and timely delivery, of electronic mail is also not
guaranteed. Legg Mason therefore recommends that you do not send
time-sensitive
or action-oriented messages to us via electronic mail, including
authorization to "buy" or "sell" a security or instructions to conduct any
other financial transaction. Such requests, orders or instructions will
not be processed until Legg Mason can confirm your instructions or
obtain appropriate written documentation where necessary.
IMPORTANT: The security of electronic mail sent through the Internet
is not guaranteed. Legg Mason therefore recommends that you do not
send confidential information to us via electronic mail, including social
security numbers, account numbers, and personal identification numbers.

Delivery, and timely delivery, of electronic mail is also not
guaranteed. Legg Mason therefore recommends that you do not send time-sensitive
or action-oriented messages to us via electronic mail, including
authorization to "buy" or "sell" a security or instructions to conduct any
other financial transaction. Such requests, orders or instructions will
not be processed until Legg Mason can confirm your instructions or
obtain appropriate written documentation where necessary.



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:17:20 EDT