delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2010/01/14/20:39:27

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=BAYES_00,SPF_PASS
X-Spam-Check-By: sourceware.org
To: "cygwin AT cygwin DOT com" <cygwin AT cygwin DOT com>
Subject: Re: Can't set variables in a while loop that is passed to the rest of the script.
References: <EDF49EC9787F914CA157FF6927D65A88302633AB82 AT CBMCC-X7-MBX10 DOT ad DOT cibc DOT com> <4B4FAB5D DOT 4000106 AT bopp DOT net>
Date: Fri, 15 Jan 2010 02:39:12 +0100
MIME-Version: 1.0
From: "Matthias Andree" <matthias DOT andree AT gmx DOT de>
Message-ID: <op.u6jv7ms71e62zd@merlin.emma.line.org>
In-Reply-To: <4B4FAB5D.4000106@bopp.net>
User-Agent: Opera Mail/10.10 (Linux)
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Am 15.01.2010, 00:40 Uhr, schrieb Jeremy Bopp <jeremy AT bopp DOT 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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019