Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Discuss] Bourne Shell variable assignment question



Thanks guys.
I have several workable solutions for him. The underlying issue is that
he wants to prevent malicious code.  In the context he is working on, I
don't think that is a risk, but using egrep to reject any lines that are
not in the form 'var=value'. I also think that his script must be
Bourne, not BASH.

On 12/15/2011 04:51 PM, Chris Tyler wrote:
> On Thu, 2011-12-15 at 16:35 -0500, Jerry Feldman wrote:
>> On 12/15/2011 04:27 PM, Richard Pieri wrote:
>>> On 12/15/2011 4:25 PM, Peter Doherty wrote:
>>>> Of course, one has to ask why your co-worker is doing this, and not
>>>> just sourcing the file.
>>>> Also, my example will break if you have an equal sign in your
>>>> variable name or value name.
>>> This.  Sourcing the file is going to be the fastest and most reliable
>>> way to do it.
>>>
>> This is 100% agreed. The issue is that he does not want to source the
>> file and I have not been able to talk him out of it. In the past I have
>> written scripts where you could read the name of a variable, and then
>> convert it to a variable name, but I don't think I've done it in Bourne
>> of BASH.
>
> There are lots of options:
>
> #!/bin/sh
> # equivalent to sourcing the file
> while read LINE
> do
> 	eval "$LINE"
> done <datafile
>
>
> #!/bin/bash
> # same as above, bash/ksh only
> for LINE in $(<datafile)
> do
> 	eval "$LINE"
> done
>
> #!/bin/sh
> # similar, but rejects lines that are not NAME=VALUE or have spaces
> # also exports all variables processed
> egrep "^[^= ]+=[^= ]+$" datafile|while read LINE
> do
>         eval "export $LINE"
> done
>
>
> You could easily parse out the name and value separately, but there's no
> reason to, because you're going to end up putting them on either side of
> an equal sign in an eval anyways.
>
> -Chris
>
> _______________________________________________
> Discuss mailing list
> Discuss at blu.org
> http://lists.blu.org/mailman/listinfo/discuss
>


-- 
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix
PGP key id:3BC1EB90 
PGP Key fingerprint: 49E2 C52A FC5A A31F 8D66  C0AF 7CEA 30FC 3BC1 EB90





BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org