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



On Thu, Dec 15, 2011 at 4:12 PM, Matt Shields <matt at mattshields.org> wrote:

> On Thu, Dec 15, 2011 at 3:57 PM, Jerry Feldman <gaf at blu.org> wrote:
>
>> I have not done my homework on this as much as I should.
>> A coworker needs to set variable names and values input from another
>> file. Normally, I would source that file, but he specifically wants to
>> parse the file.
>> So, in simple terms, he has a file that has something like:
>> var1=foo
>>
>> Instead of sourcing he wants to parse the file using readline so he
>> reads the variable name, then he wants to assign a variable of the same
>> name.
>> So, in his code he has something like
>> readline
>> ... - code to parse the line
>> Where varname contains the variable name(eg var1), and value contains
>> the value(eg foo)
>>
>> --
>> 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
>>
>>
>>
>> _______________________________________________
>> Discuss mailing list
>> Discuss at blu.org
>> http://lists.blu.org/mailman/listinfo/discuss
>>
>>
> Maybe not the most elegant way, but it works.  See below
>
> Matts-MacBook-Pro:temp matt$ cat test1.sh
> #!/bin/bash
> var1=dog
> var2=cat
> var3=cow
>
> Matts-MacBook-Pro:temp matt$ cat test2.sh
> #!/bin/bash
> myvar=`cat test1.sh | grep var2 | cut -d"=" -f2`
> echo $myvar
>
> Matts-MacBook-Pro:temp matt$ bash test2.sh
> cow
>
> Matthew Shields
> Owner
> BeanTown Host - Web Hosting, Domain Names, Dedicated Servers, Colocation,
> Managed Services
> www.beantownhost.com
> www.sysadminvalley.com
> www.jeeprally.com
> Like us on Facebook <http://www.facebook.com/beantownhost>
> Follow us on Twitter <https://twitter.com/#!/beantownhost>
>
>
>
Sorry, first file should be the follow.  That's why the output was wrong.

Matts-MacBook-Pro:temp matt$ cat test1.sh
#!/bin/bash
var1=dog
var2=cow
var3=cat



Matthew Shields
Owner
BeanTown Host - Web Hosting, Domain Names, Dedicated Servers, Colocation,
Managed Services
www.beantownhost.com
www.sysadminvalley.com
www.jeeprally.com
Like us on Facebook <http://www.facebook.com/beantownhost>
Follow us on Twitter <https://twitter.com/#!/beantownhost>



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