开发者

Set environment variable from outside bash

I'm trying to set a bash environment variable using PHP (from command line) with no success.

$buff=array();
$buff[]="VARTESTKEY=VARTESTVALUE";
$buff[]="export VARTESTKEY";
file_put_contents('script.sh', implode("\n",$buff));
system('source script.sh');

I've even tried using a script to output the key value which gets evaled:

$buff=array();
$buff[]="echo VARTESTKEY=VARTESTVALUE";
file_put_contents('script.sh', implode("\n",$buff));
system('eval "$(bash script.sh)"');

But still nothing.

Any ideas? I don't mind using any开发者_如何学Go other tool (perl, python, c, etc.) as long as it can do its job by being called from the PHP system function.


Do you need this environment variables before running another bash script?

You can just use putenv("KEY=VAL");

Es:

<?php
putenv("ASD=LOL");
system("echo \$ASD");
?>

Edit:

<?php 
echo "VARTESTKEY=VARTESTVALUE";
?>

launch it as:

$ eval `php script.php` && echo $VARTESTKEY
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜