0

Im building a shell script to automate some process on my work.

I need to pull some variables from a php they are created like this:

return [

    'db' => [
        'table_prefix' => '',
        'connection' => [
            'default' => [
                'host' => 'localhost',
                'dbname' => 'dbname',
                'username' => 'root',
                'password' => '',
                'model' => 'mysql4',
                'engine' => 'innodb',
                'initStatements' => 'SET NAMES utf8;',
                'active' => '1'
            ]
        ]
    ],

So, my plan was to pull the php file this way:

#!/bin/bash  
# php file to get variables from
FILE_NAME="etc/env.php"
echo "Starting Migration..."  
DATABASE=$(cat $FILE_NAME | grep "^\'dbname' =>" | cut -d "=" -f 2-) 
DATABASE_HOST=$(cat $FILE_NAME | grep "^\host=" | cut -d "=" -f 2-)
DATABASE_PASS=$(cat $FILE_NAME | grep "^\password=" | cut -d "=" -f 2-)
DATABASE_USER=$(cat $FILE_NAME | grep "^\$username=" | cut -d "=" -f 2-)
echo "Value from PHP file"
echo "Database name ${DATABASE}"
echo "Database host ${DATABASE_HOST}"
echo "Database pass ${DATABASE_PASS}"
echo "Database user ${DATABASE_USER}"

But im not getting the variables , its just empty...any ideas?

  • Is FILE_NAME supposed to be "etc/env.php" or "/etc/env.php"? Do any of the subexpressions work? What does cat $FILE_NAME | grep "^\'dbname' =>" | cut -d "=" -f 2- do on its own? Why cat $FILE_NAME | grep expression? grep expression $FILE_NAME is better. There's porkmail.org/era/unix/award.html – waltinator Dec 4 '19 at 18:08
  • Yes, this command DATABASE=$(cat $FILE_NAME | grep "dbname" | cut -d "=" -f 2-) seems to be working, but im getting some characters i dont need, im getting with that call: > 'dbname', , so i just need to get dbname – lucasvm1980 Dec 4 '19 at 18:10
  • You could grep expression $FILE_NAME | cut "-d'" -f4, that is use "'" as the delimiter. – waltinator Dec 4 '19 at 18:29
2

Using GNU grep if available, you can do this

grep -Po "(?<='dbname' => ).*(?=,)" file

In your script:

DATABASE=$(grep -Po "(?<='dbname' => ).*(?=,)" $FILE_NAME) 
DATABASE_HOST=$(grep -Po "(?<='host' => ).*(?=,)" $FILE_NAME) 
DATABASE_PASS=$(grep -Po "(?<='password' => ).*(?=,)" $FILE_NAME) 
DATABASE_USER=$(grep -Po "(?<='username' => ).*(?=,)" $FILE_NAME) 

With the -P option you activate Perl-like regular expressions.

With the -o option you get only the captured string, not the whole line.

Basically this happens:

(?<=<string starts with>)<capture this string>(?=<string ends with>)
  • This is close, im getting etc/env.php:'localhost' , i need get only localhost. – lucasvm1980 Dec 4 '19 at 18:47
  • Sorry I edited the answer, there was an error: file $FILE_NAME is just $FILE_NAME – guillermo chamorro Dec 4 '19 at 18:51
  • Thank you, its working, just getting: Database host 'localhost' can we remove the ' and just get Database host localhost ? can you explain what are you doing? – lucasvm1980 Dec 4 '19 at 19:03
  • I edited the answer, hopefully it's clear. To remove the single quotes you just need "(?<='dbname' => ').*(?=',)". Do you want me to add that to the answer? – guillermo chamorro Dec 4 '19 at 19:09
  • Thank you so much, this is the final result: DATABASE=$(grep -Po "(?<='dbname' => ').*(?=',)" $FILE_NAME) DATABASE_HOST=$(grep -Po "(?<='host' => ').*(?=',)" $FILE_NAME) DATABASE_PASS=$(grep -Po "(?<='password' => ').*(?=',)" $FILE_NAME) DATABASE_USER=$(grep -Po "(?<='username' => ').*(?=',)" $FILE_NAME) – lucasvm1980 Dec 4 '19 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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