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?

3
  • Have you checked it is the correct path? Try with absolute path to see if config.sh is requested properly. Commented Apr 18, 2013 at 9: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 Commented Apr 18, 2013 at 9:18
  • I found a related question -> stackoverflow.com/questions/10302958/… Commented Apr 18, 2013 at 9:22

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.