You are not logged in.

#1 2010-06-09 13:41:54

Surgat_
Member
Registered: 2007-08-08
Posts: 317

How to get the value of a PHP variable from a bash script

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

#2 2010-06-09 13:45:04

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: How to get the value of a PHP variable from a bash script

You want a real parser, and how hard that is to make will depend on how complex the config file is.

Offline

#3 2010-06-09 14:13:23

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: How to get the value of a PHP variable from a bash script

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.

Offline

#4 2010-06-09 23:25:32

Surgat_
Member
Registered: 2007-08-08
Posts: 317

Re: How to get the value of a PHP variable from a bash script

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 yikes

Offline

#5 2010-06-10 00:35:24

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: How to get the value of a PHP variable from a bash script

Surgat_ wrote:

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 yikes

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 StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#6 2010-06-10 14:14:00

Surgat_
Member
Registered: 2007-08-08
Posts: 317

Re: How to get the value of a PHP variable from a bash script

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

#7 2010-06-10 19:09:21

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: How to get the value of a PHP variable from a bash script

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 StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#8 2010-06-12 15:17:02

Surgat_
Member
Registered: 2007-08-08
Posts: 317

Re: How to get the value of a PHP variable from a bash script

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

Board footer

Powered by FluxBB