[英]How to access PHP environment variables in a bash script?
I'm writing a bash script meant to run on a remote AMP stack. 我正在写一个bash脚本,意在在远程AMP堆栈上运行。 The script needs to access a PHP predefined environment variable ($_ENV).
该脚本需要访问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"
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. 并且为了完整的答案,我假设,php不起作用因为你注意到
Use of undefined constant PATH
,你应该包装你的字符串参数。
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. 更新:使用SSH登录时未获取.bashrc。 You need to source it in your .bash_profile like this:
您需要在.bash_profile中获取它,如下所示:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.