将变量从PHP导出到shell [英] Export a variable from PHP to shell

查看:157
本文介绍了将变量从PHP导出到shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个应该可以从PHP外部访问的变量。理想情况下,这应该是一个局部变量,但环境变量也是受欢迎的。

I'm trying to set a variable that should be accessible from outside PHP. Ideally this should be a local variable, but environment variables are also welcome.

首先,我尝试过 putenv(),但这不会得到结果:

First, I've tried putenv(), but this gives no result:


$ php -rputenv('PHP_TEST = string'); ; echo $ PHP_TEST

$ php -r "putenv('PHP_TEST=string');" ; echo $PHP_TEST

$

当我调用 getenv()从同一个脚本 - 它会导致正确的'string'值。
安全模式是关闭的,但手册说PHP_前缀对于safe = on至关重要,所以我用它只是为了:)

When i call getenv() from the same script — it results in the right 'string' value. Safe mode is off, but the manual says 'PHP_' prefix is vital with safe=on so I use it just in case :)

然后我尝试 system() shell_exec()


$ php -rshell_exec('PHP_TEST = string'); ; echo $ PHP_TEST

$ php -r "shell_exec('PHP_TEST=string');" ; echo $PHP_TEST

$ php -rshell_exec('export PHP_TEST = string'); ; echo $ PHP_TEST

$ php -r "shell_exec('export PHP_TEST=string');" ; echo $PHP_TEST

$

有解决方法吗?可能是什么原因?
我正在使用Ubuntu Linux 9.10Karmic,但是FreeBSD服务器提供相同的结果。

Is there a workaround? what might be the reason? I'm using Ubuntu Linux 9.10 "Karmic", but FreeBSD server gives the same result.

推荐答案

'尝试传递一些输出到一个shell变量,你可以这样做:

If you're trying to pass some output to a shell variable, you can do it like this:

$ testvar=$(php -r 'print "hello"')
$ echo $testvar
hello

显示导出如何影响事物:

Showing how export affects things:

$ php -r '$a=getenv("testvar"); print $a;'
$ export testvar
$ php -r '$a=getenv("testvar"); print $a;'
hello

在这些示例中,交互式shell是父进程其他一切都是一个孩子(和彼此的兄弟姐妹)。

In these examples, the interactive shell is the parent process and everything else shown is a child (and siblings of each other).

这篇关于将变量从PHP导出到shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆