1

I am writing my first BASH script to automate configuration of my (Laravel) web projects.

I have some config files (app/config/local/database.php,app/config/app.php) with PHP arrays that I need to access and modify. For example ...

'providers' => array(
    /** Append new service provider value, if it does not exist already */
    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    // ...
)

... or ...

'mysql' => array(
    /** Replace value under key "database" to "test_db" */
    'database' => 'homestead',
    'username' => 'homestead',
)

So far I was using sed expressions like this:

$LV_DB_NAME="test_db"
$LV_DB_FILE="app/config/local/database.php"
gsed -i "s/'database' .*/'database' => '$LV_DB_NAME',/g" $LV_FILE_DB_CONFIG

This feels a little messy to me, especially in the case of example 1.

What would be awesome

Is there any way to get PHP arrays to BASH arrays and work with like you would in PHP?

Example 1

if (!in_array($new_provider, $providers)) {
    $providers[] = $new_provider;
}

Example 2

$config['mysql']['database'] = $database_name

Update: What would also be awesome

If there is any other common way how to modify PHP arrays using terminal, I would be glad if you point me to it! I'm sure I'm not the only one who needs to modify PHP configuration arrays using terminal.

4
  • AFAIK you can't do that, and also I'm wondering why use bash? Can't you make your script in PHP? Then you could be handling your arrays like... PHP arrays :-) Commented Aug 17, 2014 at 16:51
  • @mTorres: My bash script does a lot of "bash stuff" - utilising Composer, Vagrant commands, etc. It would be nice, if I could do all the grunt work linked with bootstraping a project from a single script. Commented Aug 17, 2014 at 16:57
  • If you think that sed expression is messy, you probably shouldn't use sed. Commented Aug 17, 2014 at 22:19
  • Well, That is the purpose of my question. I think sed is messy in this particular situation. Can you guide me to something I should use? Commented Aug 18, 2014 at 8:11

1 Answer 1

3

mTorres was actually right. If your machine has php cli installed (which is probable), You can easily jump into PHP from your bash scripts. There are multiple ways to do so, I finally settled with this:

print_s "Putting data to file \"$PATH\" ... "
export PATH=$PATH
export DATA_JSON=$DATA_JSON

/usr/bin/php << 'EOF'

<?php

    $path = getenv("PATH");
    $data = getenv("DATA_JSON");

    $data = json_decode($data);
    $config = (file_exists($path) && is_array($config_data = require($path))) ? $config_data : array();

    foreach ($data as $k => $v) {
        $config[$k] = $v;
    }

    file_put_contents($path, "<?php \n \n return ".var_export($config,true).";");

?>

EOF

}

There are some gotchas with passing associative arrays in BASH, check my other question: Pass BASH associative arrays to PHP script

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

Comments

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.