How to access PHP environment variables in bash script?

I am writing a bash script designed to run on a remote AMP stack. The script must access the predefined PHP 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: "

None of the methods work, both variables return empty. I know that this PHP variable is set, because when I enter it into the terminal (after ssh'ing to 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 to work in my script:

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

Does anyone know what I'm doing wrong? \

*** UPDATE *******

I should mention that I call this script from my local machine as follows:

ssh -t user@mydomain.com "myscript backup_remote_db"

"myscript" - bash script, "backup_remote_db" - , , .

, , $USER script, , ...

*** 2 ******

:

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

script:

ssh -t user@mydomain.com ". /etc/profile; myscript backup_remote_db"
+4
1

php

:

 echo "The DB Host is $DATABASE_SERVER"

, , php , Use of undefined constant PATH, . :

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

Update: .bashrc SSH. .bash_profile :

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi
+4

Source: https://habr.com/ru/post/1539751/


All Articles