This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Can't set variables in a while loop that is passed to the rest of the script.


Am 15.01.2010, 00:40 Uhr, schrieb Jeremy Bopp <jeremy@bopp.net>:

On 1/14/2010 5:23 PM, Damo, David wrote:
Hi,

I have fixed the problem. It seems in cygwin it spawns a subshell even under bash. I used a for loop instead and everything worked nicely.

for line in `sed 's/\$/^/g' $propfile`
do
	nvpair=$(echo $line | awk -F"=" '{print $1,$2}')
	set -- $nvpair
	if [ ! "$1" = ""  ]; then
		eval "$1"=\"$2\"
	fi
done

Interesting. Your workaround should work fine as long as the propfile is not too large, I think. However, wouldn't it be easier to source a properly massaged version of propfile instead?

The observed behaviour ("sed ... | while read foo" not setting variables properly) is expected and documented, because the commands in a pipe will be launched in a subshell (per POSIX), where "in the current shell" is an exception you cannot rely on - as observed.
See IEEE Std 1003.1-2008, Shell & Utilities, Shell Command Language, 2.12 Shell Execution Environment (near the end of the section).


I'd consider using the for line a fix, rather than a workaround.

I'd also recommend a good portion of caution when sourcing generated files. This all too easily becomes a major security concern unless combined with thorough error checking. "set -e" is a bare necessity, and security isn't halfway complete there...

--
Matthias Andree

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]