let built-in doesn"t work as expected
let built-in doesn"t work as expected
am 18.04.2008 07:35:46 von Jeenu
Hi,
I have a file (myfile) containing the following lines:
a = 1
b = 2
c = 3
now if I do:
cat myfile | while read LINE; do
let "$LINE"
done
AFAIK, I should get the environment variables a, b and c, but it these
variables don't get assigned at all. Could somebody please tell me why
this is happening? Or isn't this the right way to use the 'let' built-
in?
Thanks
Jeenu
Re: let built-in doesn"t work as expected
am 18.04.2008 09:45:36 von Janis Papanagnou
On 18 Apr., 07:35, Jeenu wrote:
> Hi,
>
> I have a file (myfile) containing the following lines:
>
> a =3D 1
> b =3D 2
> c =3D 3
>
> now if I do:
>
> cat myfile | while read LINE; do
> =A0 =A0 let "$LINE"
> done
>
> AFAIK, I should get the environment variables a, b and c, but it these
> variables don't get assigned at all.
Is there a difference in your (which one?) shell when doing...
while read LINE; do
let "$LINE"
done
Janis
> Could somebody please tell me why
> this is happening? Or isn't this the right way to use the 'let' built-
> in?
>
> Thanks
> Jeenu
Re: let built-in doesn"t work as expected
am 18.04.2008 10:07:18 von skye.shaw
On Apr 17, 10:35=A0pm, Jeenu wrote:
> Hi,
>
> I have a file (myfile) containing the following lines:
>
> a =3D 1
> b =3D 2
> c =3D 3
>
> now if I do:
>
> cat myfile | while read LINE; do
> =A0 =A0 let "$LINE"
> done
>
> AFAIK, I should get the environment variables a, b and c, but it these
> variables don't get assigned at all. Could somebody please tell me why
> this is happening? Or isn't this the right way to use the 'let' built-
> in?
>
> Thanks
> Jeenu
What are you trying to do?
let is for arithmetic evaluation, Do you mean set?
[sshaw@localhost ~]$ cat bs.sh
y=3D1
x=3D$y+23
echo $x
let x=3D$y+23
echo $x
a=3D1
echo $a
a =3D 1 #oops, extra spaces
[sshaw@localhost ~]$ bash bs.sh
1+23
24
1
bs.sh: line 8: a: command not found
For assignments to bee seen by other processes, you must export them
via export or set -a
-Skye
Re: let built-in doesn"t work as expected
am 18.04.2008 16:15:13 von Janis Papanagnou
On 18 Apr., 10:07, "Skye Shaw!@#$" wrote:
> On Apr 17, 10:35=A0pm, Jeenu wrote:
>
>
>
>
>
> > Hi,
>
> > I have a file (myfile) containing the following lines:
>
> > a =3D 1
> > b =3D 2
> > c =3D 3
>
> > now if I do:
>
> > cat myfile | while read LINE; do
> > =A0 =A0 let "$LINE"
> > done
>
> > AFAIK, I should get the environment variables a, b and c, but it these
> > variables don't get assigned at all. Could somebody please tell me why
> > this is happening? Or isn't this the right way to use the 'let' built-
> > in?
>
> > Thanks
> > Jeenu
>
> What are you trying to do?
> let is for arithmetic evaluation, Do you mean set?
$ cat myfile
a =3D 1
b =3D 2
c =3D 3
$ cat myfile.sh
while read LINE; do
let "$LINE"
done
echo $a
echo $b
echo $c
$ ksh myfile.sh
1
2
3
Janis
>
> [sshaw@localhost ~]$ cat bs.sh
> y=3D1
> x=3D$y+23
> echo $x
> let x=3D$y+23
> echo $x
> a=3D1
> echo $a
> a =3D 1 #oops, extra spaces
>
> [sshaw@localhost ~]$ bash bs.sh
> 1+23
> 24
> 1
> bs.sh: line 8: a: command not found
>
> For assignments to bee seen by other processes, you must export them
> via export or set -a
>
> -Skye- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -
Re: let built-in doesn"t work as expected
am 18.04.2008 18:32:12 von cfajohnson
On 2008-04-18, Jeenu wrote:
> Hi,
>
> I have a file (myfile) containing the following lines:
>
> a = 1
> b = 2
> c = 3
>
> now if I do:
>
> cat myfile | while read LINE; do
> let "$LINE"
> done
>
> AFAIK, I should get the environment variables a, b and c, but it these
> variables don't get assigned at all. Could somebody please tell me why
> this is happening? Or isn't this the right way to use the 'let' built-
> in?
There are two problems with your script. The first is that the
elements of the pipe are executed in subshells and will not change
anything elsewhere in the script. The second is that there should
be no spaces around the equals signs.
The first problem can be solved by using redirection instead of
cat; the second by reading the components of each line:
while read var op val; do
case $op in
=) eval "$var=\$val";;
esac
done < "$FILE"
A better way would be to structure the file so that it can be
sourced:
a=1
b=2
c=3
.. "$FILE"
--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Re: let built-in doesn"t work as expected
am 18.04.2008 19:14:39 von Dave B
On Friday 18 April 2008 18:32, Chris F.A. Johnson wrote:
> anything elsewhere in the script. The second is that there should
> be no spaces around the equals signs.
There can indeed be spaces, *iff* the lines are passed as arguments to let
(which, agreed, is not a standard command AFAIK, but that's what the OP was
trying to use). The caveat is that, as with everything that needs to be
treated as a single entity in the shell, it must be quoted.
$ asg="a = 12"
$ let "$asg"
$ echo $a
12
$ asg="a = 14 + 12"
$ let "$asg"
$ echo $a
26
--
D.
Re: let built-in doesn"t work as expected
am 18.04.2008 23:33:23 von Dan Mercer
"Jeenu" wrote in message
news:7ef246bb-b564-4666-b18d-45feab07b953@c58g2000hsc.google groups.com...
> Hi,
>
> I have a file (myfile) containing the following lines:
>
> a = 1
> b = 2
> c = 3
>
> now if I do:
>
> cat myfile | while read LINE; do
> let "$LINE"
> done
>
This is a UUOC - a useless use of cat. I presume you are using bash, and
bash runs pipelines
in a subshell. A subshell is a separate process with its own environment
stack and changing that
environment does nothing for the parent process's environment.
AT&T ksh, zsh and posix shells derived from ksh (aix, hp-ux...) all run the
last segment of
a pipeline in the current process. In those shells your construct would
work - but it would
still be wrong.
Use redirection:
while read LINE
do
let $LINE
done < myfile
Dan Mercer
> AFAIK, I should get the environment variables a, b and c, but it these
> variables don't get assigned at all. Could somebody please tell me why
> this is happening? Or isn't this the right way to use the 'let' built-
> in?
>
> Thanks
> Jeenu
Re: let built-in doesn"t work as expected
am 18.04.2008 23:43:06 von Dan Mercer
"Chris F.A. Johnson" wrote in message
news:cedbd$4808cd0c$cef88ba3$7417@TEKSAVVY.COM...
> On 2008-04-18, Jeenu wrote:
>> Hi,
>>
>> I have a file (myfile) containing the following lines:
>>
>> a = 1
>> b = 2
>> c = 3
>>
>> now if I do:
>>
>> cat myfile | while read LINE; do
>> let "$LINE"
>> done
>>
>> AFAIK, I should get the environment variables a, b and c, but it these
>> variables don't get assigned at all. Could somebody please tell me why
>> this is happening? Or isn't this the right way to use the 'let' built-
>> in?
>
> There are two problems with your script. The first is that the
> elements of the pipe are executed in subshells and will not change
> anything elsewhere in the script. The second is that there should
> be no spaces around the equals signs.
Not if you are using let. Which is probably why he chose to use let
in the first place. It's just a redirection problem.
Dan Mercer
>
> The first problem can be solved by using redirection instead of
> cat; the second by reading the components of each line:
>
> while read var op val; do
> case $op in
> =) eval "$var=\$val";;
> esac
> done < "$FILE"
>
> A better way would be to structure the file so that it can be
> sourced:
>
> a=1
> b=2
> c=3
>
> . "$FILE"
>
>
>
> --
> Chris F.A. Johnson, author
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence
Re: let built-in doesn"t work as expected
am 18.04.2008 23:47:37 von Dan Mercer
"Dan Mercer" wrote in message
news:z6WdnVxWf6IAjpTVnZ2dnUVZ_gednZ2d@comcast.com...
>
> "Jeenu" wrote in message
> news:7ef246bb-b564-4666-b18d-45feab07b953@c58g2000hsc.google groups.com...
>> Hi,
>>
>> I have a file (myfile) containing the following lines:
>>
>> a = 1
>> b = 2
>> c = 3
>>
>> now if I do:
>>
>> cat myfile | while read LINE; do
>> let "$LINE"
>> done
>>
>
> This is a UUOC - a useless use of cat. I presume you are using bash, and
> bash runs pipelines
> in a subshell. A subshell is a separate process with its own environment
> stack and changing that
> environment does nothing for the parent process's environment.
>
> AT&T ksh, zsh and posix shells derived from ksh (aix, hp-ux...) all run
> the last segment of
> a pipeline in the current process. In those shells your construct would
> work - but it would
> still be wrong.
>
> Use redirection:
>
> while read LINE
> do
> let $LINE
Whoops, left off the ""
let "$LINE"
otherwise let complains. You could also
(($LINE))
Dan Mercer
> done < myfile
>
> Dan Mercer
>
>> AFAIK, I should get the environment variables a, b and c, but it these
>> variables don't get assigned at all. Could somebody please tell me why
>> this is happening? Or isn't this the right way to use the 'let' built-
>> in?
>>
>> Thanks
>> Jeenu
>