0

I found some questions about processing an ini/bash file.

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

However they doesn't take in account other kind of assignments like:

  • arrays:
    stuff=( "foo" "bar" "donald duck" "mickey mouse" )

  • variables calling another variable:
    foobar = "bartender"
    foo = "$foobar give me a beer!"

  • possible other things I'm not figuring them atm

Instead of parsing the variables in php side i was thinking instead of using a shell script processing an input config.sh file and prepare a vars.php files already written in php language.

So an array like the one above will be converted in $stuff = array ("foo", "bar", "donald duck", "mickey mouse" ) and the other variables like $foo = "bartender give me a beer!"

But how is possible export via bash the variables definied into a shell script in an efficient way ? Has bash something like php $GLOBALS in order to cycle throught the definied vars ?

I think also an intelligent bash script "sh variables to json" could work, so that the json could be parsed in php after :)


To be more clear, I'm writing a set of scripts that are based on a config.sh file. But, i have a slight amount of php scripts also that need the same configuration, that's why I tought would have been easier prepend all my php script with a parse of the config.sh file so I can use the same vars I have in bash also in php.

While some parser of ini/bash definition exists they are mere literal parser without taking in account variables inside other variables and arrays.

Community
  • 1
  • 1
user3450548
  • 183
  • 11
  • Why write it in Bash? You could write a PHP file that could do this and then cron it. – Machavity Mar 22 '16 at 12:43
  • @Machavity uh? How cron is involved here? I need to get inside php all the vars i can from a shell file, now I'm figuring a way inside php directly or making a shell script able to process first the origina.sh file and then in php import the vars directly. – user3450548 Mar 22 '16 at 13:07
  • I read your question three times, but I still don't get it, sorry. Can you describe the problem you'd like to solve? Why are your variables in a shell script when the rest of your software is in PHP? As far as `$GLOBALS` is concerned, try `env` (lists environment variables) and `set` (lists shell variables and functions). – Michael Jaros Mar 22 '16 at 13:43
  • 1
    @MichaelJaros, edited the question adding more clarification. – user3450548 Mar 22 '16 at 17:29
  • PHP has a variable called `$_ENV`, providing access to all environment variables. If you source your configuration before calling PHP, you will have access to environment variables declared with `export`. Variables inside assigned statements (`foo=footext $bar`) will be substituted. But it is not possible to export arrays in Bash. Generally I'd recommend to avoid arrays in Bash. The syntax is clumsy and there are limitations like the one just mentioned. – Michael Jaros Mar 22 '16 at 18:46
  • @MichaelJaros I see, thank you for the tip, at this point probably the best solution will be create a php file with the config and use php to generate a more simplier config.sh for the remaining bash scripts. – user3450548 Mar 23 '16 at 00:05

0 Answers0