4

I'm writing a bash script meant to run on a remote AMP stack. The script needs to access a PHP predefined environment variable ($_ENV).

This is what I want:

db_host=$(php -r 'echo $_ENV{DATABASE_SERVER};')
echo "The DB Host is: $db_host"
# output: "The DB Host is: internal-db.s173785.gridserver.com"

This is what I get instead:

# METHOD 1
db_host1=$(php -r 'echo $_ENV{DATABASE_SERVER};')
echo "The DB Host is: $db_host1"
# output: "The DB Host is: "

# METHOD 2
db_host2=`php -r 'echo get_env(DATABASE_SERVER);'`
echo "The DB Host is: $db_host2"
# output: "The DB Host is: "

Neither method works, both variables return empty. I know that this PHP variable is set, because when I type this into the terminal (after ssh'ing into the server), I get the expected value:

$ php -r 'echo $_ENV{DATABASE_SERVER};' 
# outputs: "internal-db.s173785.gridserver.com"

Technically the above methods should work, because I managed to get this working in my script:

php_user=$(php -r 'echo getenv("USER");')
echo php_user is $php_user
# outputs: "php_user is myusername"

Anyone know what I am doing wrong?\

***UPDATE*******

I should mention that I am invoking this script from my local machine like so:

ssh -t [email protected] "myscript backup_remote_db"

"myscript" is the name of my executable bash script, and "backup_remote_db" is the function I'm passing to it which contains the code above.

This might not be cause however, because when I echo $USER in the script, it echoes the remote user, not the local one...

***UPDATE 2******

Here is how I finally got it working:

db_host=$DATABASE_SERVER
echo "The DB Host is $db_host"
# output: "The DB Host is: internal-db.s173785.gridserver.com"

But only if I make this adjustment to how the script is invoked:

ssh -t [email protected] ". /etc/profile; myscript backup_remote_db"
12
  • What prints echo $DATABASE_SERVER ? Commented May 8, 2014 at 11:32
  • 1
    Are you sure, that script runs with same environment? may be it runs from other user or something like this? Commented May 8, 2014 at 11:45
  • 1
    Where is DATABASE_SERVER set in the first place? Your .bashrc perhaps? Commented May 8, 2014 at 11:46
  • 1
    Hmm, perhaps /etc/profile then? It must be somewhere :) Commented May 8, 2014 at 11:51
  • 2
    So then you need to have . /etc/bash.bashrc in your script and then you can use $DATABASE_SERVER. Commented May 8, 2014 at 11:54

1 Answer 1

5

You don't need php for get environment variables in your shell

Just print it:

 echo "The DB Host is $DATABASE_SERVER"

And for full answer, I assume, that php doesn't work because you get notice Use of undefined constant PATH, you should wrap your string arguments.
This should work:

v=$(php -r 'print_r(getenv("DATABASE_SERVER"));')
echo "DB: $v"

Update: .bashrc is not sourced when you log in using SSH. You need to source it in your .bash_profile like this:

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi
Sign up to request clarification or add additional context in comments.

6 Comments

When using single quotes in bash's echo, the $v doesn't get interpreted (it prints DB: $v) whereas with double quotes it prints the expected output.
unfortunately this is not working for me, v still comes up blank
cat ~/.bashrc gives: No such file or directory. I can't find the .bashrc
simple change it to /etc/bash.bashrc
@vp_arth yep, this is the answer. I finally found the dotfile I needed, it was /etc/profile. I update my bash script to include . /etc/profile before running the script, and now it works. So you taught me two thing: first, environment variables exist independent of PHP. Second, there's a difference between executing a command via ssh, versus when ssh'ing into a server via command line. In the latter case, it sources all kinds of dot files behind the scenes. Thanks everyone.
|

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.