How get data back out of a (PHP) shell script

Viewed 54

I'm running a PHP script in a larger shell script by doing

php -f $filename > `basename $filename .php`.html

Now I would like to let the script tell me which file extension to use for the output file name. I tried

export AW_FILENAME_SUFFIX=".html"
php -f $filename > `basename $filename .php`$AW_FILENAME_SUFFIX

and a putenv("AW_FILENAME_SUFFIX=.txt") in the script itself, but it seems that only changes the environment the PHP command and its subprocesses run in, not the one of the calling script.

Is there another way to get data out of my script besides the output that I can use for metadata like that, or writing to a second file?

I will run several scripts, which AFAIK means I can't really source them into mine, that might give me duplicate function errors.

1 Answers

You can do the writing in your PHP script. So that is:

<?php
$ext = '.txt';
$filename = $argv[1].$ext;
ob_start();
    // my script
$data = ob_get_clean();
file_put_contents($data, $filename);

and then from the shell you can call like:

php -f $scriptname $dumpfile
Related