LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Reading a bash variable in bash scripting problem (https://www.linuxquestions.org/questions/programming-9/reading-a-bash-variable-in-bash-scripting-problem-686390/)

freeindy 11-26-2008 11:04 AM

Reading a bash variable in bash scripting problem
 
Hi,

I have a config file that contains:

my.config:
Code:

Section
INSTALLATION_PATH = $HOME
EndSection

Now in my bash script, I want to get the output /home/user instead of $HOME once read. So far, I have managed to get the $HOME variable but I can't get it to echo the variable. All I get is the output $HOME.

here is my parse_cmd script:
Code:

#Get the parameter from the section                                           
RESULT=`sed -n "/Section/,/EndSection/p" $1 | grep -w $3`                                                                           

#Ignore comment                                                               
RESULT=`echo $RESULT | awk -F# '{print $1}'`

#remove all beginning white spaces                                             
RESULT=`echo $RESULT | awk -F= '{print $NF}' | awk '{sub("^ ", "", $0); print $0}'


#return value                                                                 
echo $RESULT

exit 0

Any idea how to get around the problem.

Thanks,
Indy

colucix 11-26-2008 11:23 AM

Code:

eval echo $RESULT

jan61 11-26-2008 12:53 PM

Moin,

you can do the config parsing in one step:
Code:

jan@jack:~/tmp/config> cat cfg
Section
INSTALLATION_PATH = $HOME                # the installation path
EndSection
jan@jack:~/tmp/config> eval echo `sed -nr '/^Section/,/^EndSection/{/Section/d;s/.* *= *//;s/#.*//;p}' cfg`
/home/jan

Jan

freeindy 11-27-2008 02:29 AM

thanks colucix, easy and neat.

Jan64,
I prefer not to do everything in one line. If it needs to be modified or improved, it's better policy in my opinion to have it separated into several commands than just one. Thanks for the tip though.

Indy


All times are GMT -5. The time now is 11:22 AM.