Trouble with while, read and variables.

Trouble with while, read and variables.

Post by Simon Faire » Fri, 13 Jun 1997 04:00:00



Hi,

After tearing my hair out for a while I found the cause of my problem,
but I don't really understand it and can't seem to find a way round it.
If I enter the following:

#!/bin/sh
i=0
while read line; do
  i=`expr $i + 1`
  echo "i is $i"
  test $i -eq 10 && break
done < SomeFileWithMoreThan10Lines
echo "Finally i is $i"

Then after the while i is still 0, now I assume this has something to do
with the way 'read' works because a straight while(true) loop works
fine. I did try a variation on the 'read a line at a time' theme using:

for line in `cat file`;do
  # process line
done

but the problem with this is that it splits lines by spaces and I get
fields in the line one at a time rather than the whole line.

Any help would be greatly appreciated.

Simon

 
 
 

Trouble with while, read and variables.

Post by Herve Couppe de Lahongrais - CD » Fri, 13 Jun 1997 04:00:00



> Hi,

> After tearing my hair out for a while I found the cause of my problem,
> but I don't really understand it and can't seem to find a way round it.
> If I enter the following:

> #!/bin/sh
> i=0
> while read line; do
>   i=`expr $i + 1`
>   echo "i is $i"
>   test $i -eq 10 && break
> done < SomeFileWithMoreThan10Lines
> echo "Finally i is $i"

> Then after the while i is still 0, now I assume this has something to do
> with the way 'read' works because a straight while(true) loop works
> fine.

This is because the while loop is redirected from
SomeFileWithMoreThan10Lines.
If redirection is used in a non-simple command (for, if, while, {},
etc.)
a subshell is created to execute that command. This implies that any
shell
variables set, created, or modified within the command do not retain
their
values after execution. If no redirection is used, a subshell is not
created,
and shell modifications made within the command are preserved.

Quote:> I did try a variation on the 'read a line at a time' theme using:

> for line in `cat file`;do
>   # process line
> done

> but the problem with this is that it splits lines by spaces and I get
> fields in the line one at a time rather than the whole line.

> Any help would be greatly appreciated.

> Simon

You can try :

#!/bin/sh
i=0
{ while read line; do
        i=`expr $i + 1`
        echo "i is $i"
        test $i -eq 10 && exit $i
  done
  exit $i

Quote:} < SomeFileWithMoreThan10Lines

i=$?
echo "Finally i is $i"

--
Herve Couppe de Lahongrais (SEU)       | Eurocontrol Experimental Centre


 
 
 

Trouble with while, read and variables.

Post by Simon Faire » Fri, 13 Jun 1997 04:00:00



> Hi,

> After tearing my hair out for a while I found the cause of my problem,
> but I don't really understand it and can't seem to find a way round it.
> If I enter the following:

> #!/bin/sh
> i=0
> while read line; do
>   i=`expr $i + 1`
>   echo "i is $i"
>   test $i -eq 10 && break
> done < SomeFileWithMoreThan10Lines
> echo "Finally i is $i"

> Then after the while i is still 0, now I assume this has something to do
> with the way 'read' works because a straight while(true) loop works
> fine. I did try a variation on the 'read a line at a time' theme using:

> for line in `cat file`;do
>   # process line
> done

> but the problem with this is that it splits lines by spaces and I get
> fields in the line one at a time rather than the whole line.

> Any help would be greatly appreciated.

> Simon

After further investigation I have found that under the following OS the
script gives 'Finally i is 10':
  HPUX 10.20
  AIX 4.1.4
Whereas under the next OS it gives 'Finally i is 0':
  HPUX 9.04
  SunOS 5.3
  Irix 6.2
  OSF 3.2

I heard mention of something regarding a POSIX shell and wondered if
this was what might be running on the first two ( working ) machines?

Simon 'Now I am getting really confused'

 
 
 

Trouble with while, read and variables.

Post by peter hakans » Fri, 13 Jun 1997 04:00:00


bsd/os (2.0.1 and upwards) gives :
sh x
i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
i is 8
i is 9
i is 10
Finally i is 10  (true for sh bash and ksh)

Irix 6.3 gives 0 for sh but 10 for ksh.

In my (humble)opinion the correct answer is 10.

regards

: >
: > Hi,
: >
: > After tearing my hair out for a while I found the cause of my problem,
: > but I don't really understand it and can't seem to find a way round it.
: > If I enter the following:
: >
: > #!/bin/sh
: > i=0
: > while read line; do
: >   i=`expr $i + 1`
: >   echo "i is $i"
: >   test $i -eq 10 && break
: > done < SomeFileWithMoreThan10Lines
: > echo "Finally i is $i"
: >
: > Then after the while i is still 0, now I assume this has something to do
: > with the way 'read' works because a straight while(true) loop works
: > fine. I did try a variation on the 'read a line at a time' theme using:
: >
: > for line in `cat file`;do
: >   # process line
: > done
: >
: > but the problem with this is that it splits lines by spaces and I get
: > fields in the line one at a time rather than the whole line.
: >
: > Any help would be greatly appreciated.
: >
: > Simon

: After further investigation I have found that under the following OS the
: script gives 'Finally i is 10':
:   HPUX 10.20
:   AIX 4.1.4
: Whereas under the next OS it gives 'Finally i is 0':
:   HPUX 9.04
:   SunOS 5.3
:   Irix 6.2
:   OSF 3.2

: I heard mention of something regarding a POSIX shell and wondered if
: this was what might be running on the first two ( working ) machines?

: Simon 'Now I am getting really confused'

--
---
Peter H?kanson Volvo Technological Development. Dep 6970

 
 
 

Trouble with while, read and variables.

Post by Bill Marc » Fri, 13 Jun 1997 04:00:00


In sh, when a loop has standard input redirected, the loop becomes a
subshell.  I believe this will work:
exec 3<file
while read line; do
whatever
done <&3

--
Bill Marcum    bmarcum at iglou dot com
"I'm looking at PAGES AND PAGES of stuff even the Franklin Mint couldn't
give away for free." -- K. Mennie

 
 
 

Trouble with while, read and variables.

Post by Andrew Dunst » Fri, 13 Jun 1997 04:00:00


An overdose of spam made Herve Couppe de Lahongrais - CDL


>> Hi,

>> After tearing my hair out for a while I found the cause of my problem,
>> but I don't really understand it and can't seem to find a way round it.
>> If I enter the following:

>> #!/bin/sh
>> i=0
>> while read line; do
>>   i=`expr $i + 1`
>>   echo "i is $i"
>>   test $i -eq 10 && break
>> done < SomeFileWithMoreThan10Lines
>> echo "Finally i is $i"

>> Then after the while i is still 0, now I assume this has something to do
>> with the way 'read' works because a straight while(true) loop works
>> fine.

>This is because the while loop is redirected from
>SomeFileWithMoreThan10Lines.
>If redirection is used in a non-simple command (for, if, while, {},
>etc.)
>a subshell is created to execute that command. This implies that any
>shell
>variables set, created, or modified within the command do not retain
>their
>values after execution. If no redirection is used, a subshell is not
>created,
>and shell modifications made within the command are preserved.

>> I did try a variation on the 'read a line at a time' theme using:

>> for line in `cat file`;do
>>   # process line
>> done

>> but the problem with this is that it splits lines by spaces and I get
>> fields in the line one at a time rather than the whole line.

>> Any help would be greatly appreciated.

>> Simon

>You can try :

>#!/bin/sh
>i=0
>{ while read line; do
>        i=`expr $i + 1`
>        echo "i is $i"
>        test $i -eq 10 && exit $i
>  done
>  exit $i
>} < SomeFileWithMoreThan10Lines
>i=$?
>echo "Finally i is $i"

ksh seems to handle this in the required manner (tested on Solaris):

 >cat looper.sh
#!/bin/ksh
i=0
while read line; do
        i=`expr $i + 1`
        echo "i is $i"
        test $i -eq 10 && break
done < .profile
echo "Finally i is $i"
 >looper.sh
i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
i is 8
i is 9
i is 10
Finally i is 10
 >

you can escape doing the redirection on the loop in Bourne shell by using
exec, thus:

 >cat looper2.sh
#!/bin/sh
exec 3<&0 0<.profile
i=0
while read line; do
        i=`expr $i + 1`
        echo "i is $i"
        test $i -eq 10 && break
done
exec 0<&3 3<&-
echo "Finally i is $i"
 >looper2.sh
i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
i is 8
i is 9
i is 10
Finally i is 10
 >

cheers

andrew
--
-------------------------------------------------------------------------
  There's nothing either good or bad, but thinking makes it so - Hamlet
  http://www.gr-lakes.com/~andrew (including PGP key)
  PGP Key fingerprint = 5C 44 7D E4 76 A3 31 DE  3D 11 FA 15 4D 87 1F 5E
-------------------------------------------------------------------------

 
 
 

Trouble with while, read and variables.

Post by Matt Fost » Fri, 13 Jun 1997 04:00:00


: Hi,

{*SNIP*]

: I did try a variation on the 'read a line at a time' theme using:

: for line in `cat file`;do
:   # process line
: done

: but the problem with this is that it splits lines by spaces and I get
: fields in the line one at a time rather than the whole line.

: Any help would be greatly appreciated.

: Simon

This was an sh script so you should be able to do

IFS='
'

To change the IFS to carriage return only, rather than CR, TAB or <SP>.
Should be in sh(1).

Matt

--
| Matt Foster | Transactional Systems | DiverseyLever  |
| +44 (0)1 494 468 380 (V) | +44 (0)1 494 468 217 (F)  |
| "We'll give it logons, NTFS, TCPIP, ACLs and call it |
| a multi-user OS."  `No it's not.  It's a GUI.`       |

 
 
 

Trouble with while, read and variables.

Post by Matt Fost » Fri, 13 Jun 1997 04:00:00


: Hi,

: After tearing my hair out for a while I found the cause of my problem,
: but I don't really understand it and can't seem to find a way round it.
: If I enter the following:

: #!/bin/sh
: i=0
: while read line; do
:   i=`expr $i + 1`
:   echo "i is $i"
:   test $i -eq 10 && break
: done < SomeFileWithMoreThan10Lines
: echo "Finally i is $i"

: Then after the while i is still 0, now I assume this has something to do
: with the way 'read' works because a straight while(true) loop works
: fine. I did try a variation on the 'read a line at a time' theme using:

: for line in `cat file`;do
:   # process line
: done

: but the problem with this is that it splits lines by spaces and I get
: fields in the line one at a time rather than the whole line.

: Any help would be greatly appreciated.

: Simon

--
| Matt Foster | Transactional Systems | DiverseyLever  |
| +44 (0)1 494 468 380 (V) | +44 (0)1 494 468 217 (F)  |
| "We'll give it logons, NTFS, TCPIP, ACLs and call it |
| a multi-user OS."  `No it's not.  It's a GUI.`       |

 
 
 

Trouble with while, read and variables.

Post by Simon Faire » Sat, 14 Jun 1997 04:00:00



> Hi,
>SNIP<

Thanks to all for the responses they proved most helpful in solving my
problem and also v.educational.

Simon 'man cannot rely on Perl alone' F

 
 
 

Trouble with while, read and variables.

Post by Andrew Dunst » Wed, 18 Jun 1997 04:00:00



say:

Quote:

>In sh, when a loop has standard input redirected, the loop becomes a
>subshell.  I believe this will work:
>exec 3<file
>while read line; do
>whatever
>done <&3

still causes a subshell (at least on Solaris)

use:

exec 3<&0 0<file
while read line ; do
 ...
done
exec 0<&3 3<&-

cheers

andrew

--
-------------------------------------------------------------------------
  There's nothing either good or bad, but thinking makes it so - Hamlet
  http://www.gr-lakes.com/~andrew (including PGP key)
  PGP Key fingerprint = 5C 44 7D E4 76 A3 31 DE  3D 11 FA 15 4D 87 1F 5E
-------------------------------------------------------------------------

 
 
 

1. Reading HTTP POST Variables in Shell Variables - HOW?

Hello-
        I need to read variables posted from a www form into shell
enviromental variables.  I can do it with the GET method, but I don't want
Passwords to show up in clear-text on the Location line, thus I need to use
the POST method... Anyone have any Ideas about how to read these variables in?

Thanks in advance....

Josh

--

     -=-=-=- www.youtools.com - FASTNET(tm) PA/NJ/DE Internet -=-=-=-

2. JAVA and FreeBSD 2.1

3. ksh: Need to read variables into variables

4. SuSE 9.1 - can't communicate with router

5. Press Ctrl-D whiling inputting

6. Recycling port numbers on Solaris 2.2

7. Matrox Mystique ands X.

8. Question.

9. Setting variables in a variable array of null variables

10. "$" in environment variable causes trouble

11. awk variable troubles

12. bash variable type conversion trouble

13. trouble printing global variables with dbx