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



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
.



Relevant Pages

  • CMUCL "-script" hack now available
    ... CMUCL won't start up correctly.] ... $ cat script_header.cmucl ... If you want a script which just sets up an environment ... Welcome to the customized test REPL ...
    (comp.lang.lisp)
  • Re: Batch Cisco Configuration Script
    ... configuration of many routers but now I cannot find it unfortunately. ... The way the script would work is as follows. ... I can't use Kiwi Cat ... open telnet session. ...
    (comp.dcom.sys.cisco)
  • Re: script reports file not found
    ... any and all feedback on this script appreciated. ... when it's kicking out the cat of the file in question. ... primitive linux script to run some ruby stuff. ... why all the cat commands? ...
    (Fedora)
  • Re: My auth.log analyze script
    ... users on this group I decided to write my own auth.log analyzing script. ... rm $TMPFILE1 ... You don't need cat: ... # Sort out Unique IPs ...
    (alt.os.linux)
  • Re: Plain text file on web server, questions please...
    ... strictly for online viewing that would add the HTML tags to the online ... a utility that would do this or can it be done with a shell script ... thing, this way that if anything changed in the FAQ, it would also be ... cat head.html joke.txt tail.html> joke_$.html ...
    (alt.os.linux)