开发者

Set environment variable from outside bash

开发者 https://www.devze.com 2023-02-05 23:23 出处:网络
I\'m trying to set a bash environment variable using PHP (from command line) with no success. $buff=array();

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

精彩评论

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