2

Let's say I have a PHP file such as this:

<?php
/* Comments */

define('DB_SERVER', 'localhost');

/* More comments */

define('DB_NAME', 'dbname');
define('DB_USER', 'etc');

/* etc...*/
?>

How can I use shell script only to pull the variable values for DB_SERVER, DB_NAME, etc and store them in variables to use within the shell script? (/bin/sh, not bash)

1 Answer 1

5
# with /bin/bash
. <(awk '/define/ {print $2"="$4}' FS="'" foo.php)    
# with /bin/sh
declare `awk '/define/ {print $2"="$4}' FS="'" foo.php`

Result

echo $DB_SERVER # localhost

Example

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

1 Comment

Modified to grab only DB_ vars: . <(awk '/define.*DB_/{print...

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.