Re: let built-in doesn't work as expected
- From: "Chris F.A. Johnson" <cfajohnson@xxxxxxxxx>
- Date: Fri, 18 Apr 2008 16:32:12 +0000
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 <http://cfaj.freeshell.org/shell/>
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
.
- Follow-Ups:
- Re: let built-in doesn't work as expected
- From: Dan Mercer
- Re: let built-in doesn't work as expected
- From: Dave B
- Re: let built-in doesn't work as expected
- References:
- let built-in doesn't work as expected
- From: Jeenu
- let built-in doesn't work as expected
- Prev by Date: Re: Help required:Awk Error
- Next by Date: Re: Environment Files
- Previous by thread: Re: let built-in doesn't work as expected
- Next by thread: Re: let built-in doesn't work as expected
- Index(es):
Relevant Pages
|