let built-in doesn"t work as expected

let built-in doesn"t work as expected

am 18.04.2008 07:47:02 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 set to the
values, but 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 08:03:11 von Bill Marcum

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 set to the
> values, but 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

You should not have spaces before or after the = sign. Also, which shell
are you using? In most shells, when you use a pipe, every process in the
pipeline is executed in a subshell. In ksh or zsh, the last process in
the pipeline is not a subshell.

Re: let built-in doesn"t work as expected

am 18.04.2008 10:10:35 von PK

On Friday 18 April 2008 07:47, 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 set to the
> values, but 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?

Try this:

$ eval $(tr -d ' ' < myfile)
$ echo "$a -- $b -- $c"
1 -- 2 -- 3

Remember to use eval only if you're 100% sure that what you're going to
execute is safe.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.

Re: let built-in doesn"t work as expected

am 18.04.2008 11:04:17 von Jeenu

On Apr 18, 11:03 am, Bill Marcum wrote:
> 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 set to the
> > values, but 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
>
> You should not have spaces before or after the = sign. Also, which shell
> are you using? In most shells, when you use a pipe, every process in the
> pipeline is executed in a subshell. In ksh or zsh, the last process in
> the pipeline is not a subshell.

I forgot to mention that I was using bash. And, to my knowledge 'let'
doesn't mind having spaces around '=' sign as long as they are valid
arithmetic expression. The puzzling factor for me is that, if I
execute the command:

let 'a = 1'

on the command line, then

echo $a

prints out the value 1. As in my OP, if I do:

cat myfile | while read LINE; do
let "$LINE"
done

echo $a will produce empty output, indicating that the variables don't
exist; however, and 'echo' instead of 'let' in the above loop prints
out the lines correctly.

Re: let built-in doesn"t work as expected

am 18.04.2008 11:15:16 von PK

On Friday 18 April 2008 11:04, Jeenu wrote:

> I forgot to mention that I was using bash. And, to my knowledge 'let'
> doesn't mind having spaces around '=' sign as long as they are valid
> arithmetic expression. The puzzling factor for me is that, if I
> execute the command:
>
> let 'a = 1'
>
> on the command line, then
>
> echo $a
>
> prints out the value 1.

See this:

http://wooledge.org:8000/ArithmeticExpression

> As in my OP, if I do:
>
> cat myfile | while read LINE; do
> let "$LINE"
> done
>
> echo $a will produce empty output, indicating that the variables don't
> exist; however, and 'echo' instead of 'let' in the above loop prints
> out the lines correctly.

See this:

http://wooledge.org:8000/BashPitfalls

pitfall #7.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.