Unix/Linux
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs Unix/Linux

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 30th, 2008, 01:00 PM
BILL
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
wierd while loop result

Hi, Friends,

In the following simple code, the WHILE LP doesn't give the result I
expected. Did I do it wrong?

You can create a config.txt with 3 lines (can be anything) in it. and
then run mytest.sh

The final a is 0, instead of 3. not sure why. Is the a in the while
loop a local variable which in only available inside the loop, not
outside the loop? Thanks.

script mytest.sh
#!/bin/sh
a=0
b=0
while read line
do
a=`expr $a + 1`
echo now a is $a
done < config.txt
echo "finally a is $a"
for i in x y z
do
b=`expr $b + 1`
echo now b is $b
done
echo finally b is $b


Reply With Quote
  #2  
Old July 30th, 2008, 02:19 PM
Michael Tosch
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
wierd while loop result

BILL wrote:
Hi, Friends,
>

In the following simple code, the WHILE LP doesn't give the result I
expected. Did I do it wrong?
>

You can create a config.txt with 3 lines (can be anything) in it. and
then run mytest.sh
>

The final a is 0, instead of 3. not sure why. Is the a in the while
loop a local variable which in only available inside the loop, not
outside the loop? Thanks.
>

script mytest.sh
#!/bin/sh
a=0
b=0
while read line
do
a=`expr $a + 1`
echo now a is $a
done < config.txt
echo "finally a is $a"
for i in x y z
do
b=`expr $b + 1`
echo now b is $b
done
echo finally b is $b


The while loop is a block.
The traditional Unix sh runs this in a sub-shell.
According to the Unix principal a child process inherits all settings
and cannot give anything back but the exit code.

My observation is that Posix shells don't do this,
unless forced by a pipe.

Unlike the while loop, the for loop is never treated as a block.


--
echo imhcea\.lophc.tcs.hmo |
sed 's2\(\)\(.\{5\}\)2\2\122;s1\(.\)\(.\)1\2\11g;1s;\. ;::;2'

Reply With Quote
  #3  
Old July 30th, 2008, 09:00 PM
Sven Mascheck
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
wierd while loop result

Michael Tosch wrote:
The while loop is a block.

That's not the relevant point. It's about the redirection:

Redirection of a command (other than a simple-command) causes a sub-shell
to be created. Such non-simple commands are control structures like
"while", "for" and "if", but also "{ list;}",

My observation is that Posix shells don't do this,
unless forced by a pipe.

You can even narrow it down to traditional Bourne shells.

BTW: Using a pipeline is a different issue, though. And the usage of
"forced" here is misleading: PSIX doesn't determine whether (the last
element of) a pipeline is a separate environment or not. And among
the implementations you will find both ways.

Unlike the while loop, the for loop is never treated as a block.

Well, you should try it :)

Reply With Quote
Reply

Viewing: Web Development Archives FAQs Unix/Linux > wierd while loop result


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek