如何在bash脚本中访问PHP环境变量?

时间:2021-12-18 23:07:36

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).

我正在写一个bash脚本,意味着在远程AMP堆栈上运行。该脚本需要访问PHP预定义的环境变量($ _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变量已设置,因为当我在终端中键入它时(在ssh进入服务器之后),我得到了预期的值:

$ 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*******

*** UPDATE *******

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

我应该提一下,我正在从我的本地机器调用这个脚本,如下所示:

ssh -t user@mydomain.com "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.

“myscript”是我的可执行bash脚本的名称,“backup_remote_db”是我传递给它的函数,其中包含上面的代码。

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

然而,这可能不是原因,因为当我在脚本中回显$ USER时,它回应远程用户,而不是本地用户......

***UPDATE 2******

***更新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 user@mydomain.com ". /etc/profile; myscript backup_remote_db"

1 个解决方案

#1


4  

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

你不需要php来获取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:

对于完整的答案,我认为,php不起作用,因为你会注意到使用未定义的常量PATH,你应该包装你的字符串参数。这应该工作:

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:

更新:使用SSH登录时不会获取.bashrc。您需要在.bash_profile中获取它,如下所示:

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

#1


4  

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

你不需要php来获取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:

对于完整的答案,我认为,php不起作用,因为你会注意到使用未定义的常量PATH,你应该包装你的字符串参数。这应该工作:

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:

更新:使用SSH登录时不会获取.bashrc。您需要在.bash_profile中获取它,如下所示:

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