1

I am a newbie with shell scripting so need a few ideas on parsing a PHP file using a shell script.

Ours is a PHP project and I am improving our shell script which is used to upload code to production server.

There is one PHP config file production.settings.php which needs to be read during upload, for a few constants -

BASE_PATH (path to project root on prod server)
db_host, db_name etc. (database settings of prod database - to be used for taking a backup of the database before upload)

Question

  • How to read the value of the constants?

    They are defined like this:

    define("BASE_PATH","/path/to/project/root");
    
  • How to read the first uncommented value of the constant?
    Note - The constant may be defined more than once in the same file (let's assume the possibilty - this may happen by mistake or there may be commented instances of the line)

So far I am only able to get the number of lines containing the string define("BASE_PATH" using grep in my shell script -

cd ..
PROJECT_ROOT=$PWD
result= grep -ic 'define("BASE_PATH",' $PROJECT_ROOT'/config/main.settings.php'
echo "see"$result
  • Is this method of parsing good enough or a yml file would be better? Is there any shell command/snippet for doing this so that I can get the result by writing lesser amount of code?

Updates
Check my other questions for more details on this:-
Manipulating an array (printed by php-cli) in shell script,
Assigning values printed by PHP CLI to shell variables,
Initiating dynamic variables (variable variables) in bash shell script

2
  • 1
    Try Phing or Ant instead. These are build tools used in deployment Commented Dec 7, 2010 at 8:00
  • Thanks @Gordon I will definitely try those soon. But first I would like to develop a complete automated deployment solution of my own. Commented Dec 8, 2010 at 8:21

3 Answers 3

1

just do it using the php, then call your shell script to invoke the php script.

Assuming you have your bunch of defines defined in defs.php:

define('NAME', 'JOHN');
define('HOBBY', 'FISHING');

then create a php script get_defs.php:

require_once 'defs.php';
$const = get_defined_constants(true);
foreach($const['user'] as $k => $v) {
   echo "export $k=$v";
}

then in your shell script, run it like so:

`php get_defs.php`

What happen is, get_defs.php will output bunch of export KEY=VALUE, then shell will run those commands outputted by your php get_defs.php.

Sign up to request clarification or add additional context in comments.

2 Comments

so shell will receive the constant's values? And then I can use them as usual in my script? That looks very nice and clean. But get_defined_constants(true) seems to return E_ERROR, E_WARNING etc too. How to get only user defined constants
that's why in my foreach I use $const['user'], as that's what you defined previously. But are you using PHP4 or PHP5? as get_defined_constants(true) is only supported in 5
1

Why don't you just code with PHP CLI? That's what you understand? Also maybe you could put constants in a ini file and read them?

2 Comments

I think I will go with CLI . But is it better to use an ini file instead of a normal php file containing constants? What is the advantage?
@Sandeepan => It explains some advantages of ini devilsworkshop.org/parsing-ini-files-using-php
0

If youre comforttable with PHP then use PHP to write the shell script. IF you go this route i would move all config settings to a config file... INI, YAML, XML, whetever floats your boat. Then i would modify the bootstrap of the application that defines your constants to also read from this config file. That way you can use it in botht your script and the app without having to change it.

2 Comments

yes I want to use the same file for both my script and the app. Why should I use PHP to write the shell script? For putting the needed constants there? If I am able to correctly read the settings from shell then is it not better?
I was jsut saying if you dont knwo shell scripting well it would be easier to write it in php. I rarely actually write shell scripts in a shell language (bash, sh, etc.) - normally i use php, python, or ruby. Its just easier to do things for me. But i mean thats up to you... the shell is powerful and theres no reason you cant use it, but theres no reason not to use a lang youre more comfortable with either if it can be invoked from the CLI which PHP can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.