Register
It is currently Mon Apr 21, 2014 9:27 am

Problem with Variables from file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Aug 14, 2009 3:59 pm   

Joined: Wed Dec 03, 2008 5:19 am
Posts: 5
Hello.

I have a file test.sh , in file.txt I want save variables, the "ls" command runs well, but the other commands from the file fails, I want MP301,MP302,MP303 and MP304 be environment variables with full paths...what should I change?

If I execute MP301="pepe01.mp3" in the command line is ok.

Thanks
Kokolisso


$./mp3wrap-test2.sh
./mp3wrap-test2.sh: line 6: MP301="pepe01.mp3": command not found
./mp3wrap-test2.sh: line 6: MP302="pepe02.mp3": command not found
./mp3wrap-test2.sh: line 6: MP303="pepe03.mp3": command not found
./mp3wrap-test2.sh: line 6: MP304="pepe04.mp3": command not found
file.txt mp3wrap-test2.sh mp3wrap-test.sh


*********************BEGIN test.sh*********************************
#!/bin/bash


while read line
do
$line
done < file.txt

*********************END test.sh*********************************

And file.txt is

*********************BEGIN file.txt*********************************
MP301="pepe01.mp3"
MP302="pepe02.mp3"
MP303="pepe03.mp3"
MP304="pepe04.mp3"
ls

*********************BEGIN file.txt*********************************


Top
 Profile  
 PostPosted: Sat Aug 15, 2009 10:01 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
You can either use source function (. file) or you can use eval.
Code:
while read line; do
    eval $line
done < file.txt


or just
Code:
. file.txt

Do notice the space between . and file.txt

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Sat Aug 15, 2009 1:21 pm   

Joined: Wed Dec 03, 2008 5:19 am
Posts: 5
Hi Fredrik!

Thanks! The second option is just what I was looking for!

Josep from Spain


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP