ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to be able to parse the variable out from defined section using a tool, called get_param.sh.
Please note, if a variable is commented (#). it should be ignored
get_params.sh
Code:
#!/bin/bash
# Enough parameters passed?
if [ $# -lt 3 ]; then
echo
echo "Usage: get_param.sh <file> <section> <parameter>"
echo
exit 1
fi
#Does file exist?
if [ ! -e $1 ] ; then
echo
echo -n "Error: File "
echo -ne '\E[0;36m'"\033[2m$1\033[0m "
echo not found!
echo
exit 1
fi
#Get all the parameter from the section
RESULT=`sed -n "/$2/,/EndSection/p" $1`
#Ignore commented ones and retreive the parameter
RESULT=`echo $RESULT | sed '/^ *#/d;s/#.*//' $1 | grep -w $3`
#remove all beginning white spaces
RESULT=`echo $RESULT | awk -F= '{print $NF}' | awk '{sub("^ ", "", $0); print $0
}'`
#return value
eval echo $RESULT
exit 0
I thought my script was working but there is some bug somewhere I can't find.
#Get all the parameter from the section
RESULT=`sed -n "/$2/,/EndSection/p" $1`
The output of this sed command consists of multiple lines. Newlines are lost when assigning multiple lines to a variable (at least this way).
Because of that, the "grep" command does not work as you seem to expect:
Quote:
Originally Posted by freeindy
Code:
#Ignore commented ones and retreive the parameter
RESULT=`echo $RESULT | sed '/^ *#/d;s/#.*//' $1 | grep -w $3`
Also, here you echo text into sed and specify a file for sed to read at the same time. I think this command will read the entire file again and what is echo-ed through the pipe (|) will be ignored. So the effect of the first command is lost.
BTW after: /^ *#/d, this: s/#.*// is not needed as all lines starting with '#' with or without leading spaces will already be deleted by the first part (/^ *#/d). This shouldn't effect functioning of the script though, just as a side note.
I think it is easier done in one single sed command:
Hko, I just replace your code and it works. However, there is one small thing. I know the spaces between cause problems but there are so many config files that I don't want to go and change all of them.
The problem that still remains is...
when a variable is set like this:
Code:
VAR_A = Apple Orange Banana
the result is
Code:
VAR_A=AppleOrange Banana.
Could this be fixed? I would like the result to be:
Code:
VAR_A=Apple Orange Banana.
jschiwal, I couldn't get your to work. It just printed out the whole file
The "^[^#]" part is changed slightly from my first example. It ignores lines that have the octothorpe character before the variable.
Here is the second thing I mentioned:
Code:
$ sed -n '/Section "Three"/,/EndSection/{
/VAR/s/[[:space:]]*=[[:space:]]*/=/p }' testfile
#VAR_A=Banana
VAR_B=Plastic
It removes whitespace around the equals sign. So you could eval the results to set the variables. "eval $(...)"
This is a method I used in an ogg2mp3 script. I used ogginfo and a sed filter to produce a number of "tag=value" lines which I eval'ed to set these variables. Then I used the variables in lame to set the tags of the mp3 file that had been in the ogg file I downloaded.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.