Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best/easiest way to parse configuration parameters in Sh/Bash and php

I got in every php project (around 25!), some sh scripts that help me with routine tasks such as deployment, repo syncronizing, databases exporting/export, etc.

The sh scripts are the same for all the projects I manage, so there must be a configuration file to store diferent parameters that depend on the project:

# example conf, the sintaxys only needs to be able to have comments and be easy to edit.
host=www.host.com
[email protected]
password=xxx

I just need to find a clean way in which this configuration file can be read (parsed) from a sh script, and at the same time, be able to read those same parameters from my PHP scripts. Without having to use XML.

Do you know a good solution for this?

Guillermo

like image 539
Guillermo Avatar asked Aug 13 '10 19:08

Guillermo


1 Answers

Simply source the script conf file as another sh file!.

Example:

conf-file.sh:

# A comment
host=www.host.com
[email protected]
password=xxx

Your actual script:

#!/bin/sh

. ./conf-file.sh

echo $host $administrator_email $passwword

And the same conf-file can be parsed in PHP: http://php.net/manual/en/function.parse-ini-file.php

like image 73
pavanlimo Avatar answered Sep 24 '22 04:09

pavanlimo