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
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:then create a php script
get_defs.php:then in your shell script, run it like so:
What happen is, get_defs.php will output bunch of
export KEY=VALUE, then shell will run those commands outputted by yourphp get_defs.php.