0

How can I get value of a variable defined in a shell script? (the script is a configuration file, only contains variable-definitions) I cannot use "source" command in exec:

echo exec('var=2; echo $var'); //writes "2"
echo exec('source config.sh; echo $var'); //writes ""

How can I get value of variables defined in a shell script?

nagy.zsolt.hun
  • 6,292
  • 12
  • 56
  • 95
  • Have you checked it is the correct path? Try with absolute path to see if `config.sh` is requested properly. – fedorqui Apr 18 '13 at 09:15
  • yes, I use absolute path in my code (the problem exists that way too) - and the path is correct, the command runs well in terminal – nagy.zsolt.hun Apr 18 '13 at 09:18
  • I found a related question -> http://stackoverflow.com/questions/10302958/is-it-possible-to-source-a-sh-file-using-php-cli-and-access-exported-env-vars – fedorqui Apr 18 '13 at 09:22

1 Answers1

2

userThis worked for me just now on the terminal:

echo var=2 > shellscript
echo "<?php echo exec('source /home/user/shellscript; echo $var'); ?>" > phpscript.php
ls | grep script
phpscript.php
shellscript
php phpscript.php 
2

So I wrote the shellscript, then a php to read the shell script and ran the php script. It did just what you wanted.

Maybe you can add the other exec components to get a clue?

<?php 
echo exec('source /home/user/shellscript; echo $var',$out,$ret); 
echo "\nout0: ". $out[0]. "\nret: $ret\n";
?>

This shows me:

~]$ php phpscript.php 
2
out0: 2
ret: 0
ndasusers
  • 727
  • 6
  • 12