You are not logged in.
Hello there,
I'm writing a simple bash script to install a module for a php app. To do so, I must read the existing config file and get the value of some variables, the code looks like this:
$CFG->dbtype = 'mysql';
I can get that line by grepping "^$CFG->dbtype", but then how can I get the content of the string? I've thought of using cut, but it seems too ugly to me. Is there a better way?
Thank you all!
Offline
You want a real parser, and how hard that is to make will depend on how complex the config file is.
[git] | [AURpkgs] | [arch-games]
Offline
Daenyth is correct if you want something robust and reliable. I'm always a fan of just making it work ;P
#!/bin/bash
getvar() {
local var="$1"
sed '/^'"$var"' *= *'\''\(.*\)'\''.*$/!d;s//\1/g' "$file"
}
file='/path/php.ini'
getvar '$CFG->dbtype'
assumes values are single quoted only.
//github/
Offline
The php file is always generated the same way, so I don't think I need anything more robust, but that's not really what I want. Actually, I don't really know what that sed line does (I've never learnt sed). I want to store that mysql string in a variable.
Isn't sed supposed to modify the file passed? I tried and it didn't
Offline
The php file is always generated the same way, so I don't think I need anything more robust, but that's not really what I want. Actually, I don't really know what that sed line does (I've never learnt sed). I want to store that mysql string in a variable.
Isn't sed supposed to modify the file passed? I tried and it didn't
Sed outputs to STDOUT by default. To capture the output of brisbin's getvar function, you need to do something like this:
value="$(getvar '$CFG->dbtype')"
assuming that the function outputs what you want (I didn't really look at it).
An alternative approach to the problem would be to write a php script which loads the file and prints out the variable. You could then invoke the script in bash and store the output in a variable, e.g.
value="$(php /path/to/php_script)"
My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
I've thought of a php script, but loading that file implies starting connections to the database and a lot of other things that I don't want to do. I think I'm finally going for something like what I had first thought:
dbtype=`grep "^\$CFG->dbtype" "$1"/config.php | cut -d "'" -f 2`
This could fail if the sentence weren't at the very start of the line, but the file is generated by a script so that is not likely to happen. Also, the value of the variable is always the field number 2 when separating by ' unless there is a comment before or inside the statement, which is also very unlikely. I think it is robust enough, what do you think?
Offline
I would change it to this:
dbtype="$(grep "^\$CFG->dbtype" "$1"/config.php | cut -d "'" -f 2)"
The quotation marks will allow it to handle whitespace.
My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone
Offline
Thanks!
Edit: I think I need to scape some of those quotation marks... what about this one?
dbtype="$(grep '^\$CFG->dbtype' '$1'/config.php | cut -d \' -f 2)"
Last edited by Surgat_ (2010-06-12 15:36:49)
Offline