0

I have a project that involves php and bash scripting.

I want to outsource all user-settings to a file "settings.conf". This file should therefore contain bash-variables aswell as php-variables.

However, bash would find php code quite irritating, aswell as php couldn't cope with bash code.

What would be a best practise to make sure my php script would only take care of the php part of the file and bash would only mind about the bash part?

The settings file might look like this

### Settings-File

## php-settings
<$php
    $language    = "en";
    $url         = "http://foo.bar";
    $copyright   = "Copyleft License";
?>
## end php-settings

## bash-settings
    pathToCertainFile="/home/johndo/files/certainFile"
## end bash-settings

## EOF
8
  • PHP can run bash scripts with the system('command-here'); command, and shells can run PHP with the php command-here command. As for settings? You can set custom php-related settings (override php.ini) in a .htaccess file, and you can make both PHP and bash scripts generate a .htaccess file with contents of your choice. Helpful?
    – ionFish
    Feb 26, 2012 at 23:39
  • Thanks for your comment, but I think this is not really what I want. Do you find my question unprecise? At the moment I have some variables in the beginning of each script (just like $pathToCertainFile or $language). These settings should be edited by the users of my script. However, I want to outsource all setting-variables to just one file.
    – speendo
    Feb 26, 2012 at 23:44
  • 2
    best practices is not to have 2 languages (bash,php) use one file for settings.
    – user557846
    Feb 26, 2012 at 23:49
  • 3
    An alternative solution would be to go for a simple standard bash variable declaration (each line starts with NAME=value) and write a php parser for it. You would need to handle quotes, escapes etc. but it is probably the only practical solution if you don't want two sets of files. I don't think there's a way to integrate php style variables in bash (and it's easier writing the parser in PHP than bash)
    – Optimist
    Feb 26, 2012 at 23:55
  • 2
    why not mix in js, c and cobol, still a terrible idea. hard to maintain, debug edit, every change has to be checked im 2 languages, lots of projects require settings in multiple files. in addition there's noting you can do in bash you couldn't put in to php in the first place.
    – user557846
    Feb 26, 2012 at 23:55

2 Answers 2

2

Rather than using a single settings file that will be parsed natively by both PHP and Bash, you should store your settings in a format that makes sense for your settings, and read that file using both scripts. It doesn't need to be executable. It just needs to contain your settings.

settings.conf:

foo="this"
bar="that"
someint=123

A bash script might look like this:

#!/usr/local/bin/bash

eval `egrep '^[a-z]+=("[a-z0-9]+"|[0-9]+)$' settings.conf`

printf "foo=%s\nbar=%s\nsomeint=%s\n\n" "$foo" "$bar" "$someint"

Note that it's generally a bad idea to use eval. There are other (better) solutions in bash. I did this for expedience.

And a PHP script might look like this:

#!/usr/local/bin/php
<?php

$fh=fopen("settings.conf", "r");
while ($line=fgets($fh, 80)) {
  if (preg_match('/^[a-z]+=("[a-z0-9]+"|[0-9]+)$/', $line)) {
    $line_a=explode("=", $line);
    $conf[$line_a[0]]=$line_a[1];
  }
}

print_r($conf);

And these examples actually work:

ghoti@pc $ ./doit
foo=this
bar=that
someint=123

ghoti@pc $ ./doit.php 
Array
(
    [foo] => "this"
    [bar] => "that"
    [someint] => 123
)
ghoti@pc $ 
1

speendo, why do you want to run your cron jobs using bash? Just save your settings in PHP format, and use a PHP scripts like ghoti's doit.php above. PHP is a perfectly valid scripting language for shell and cron.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.