0

I'm trying to pass arguments back and forth, from shell to PHP, and back again. In my simple test, I'm getting easily from shell into PHP, but having no luck getting strings back out. I've tried both escapeshellarg and escapeshellcmd, but neither worked.

Shell

#!/bin/sh

# set a date and time variables, in the form of  YYYY-MM-DD and HH:MM:SS
dateVar=`date +%Y-%m-%d`
timeVar=`date +%H_%M`

# Launching PHP
runPHP=`which php`
$runPHP calledPHP.php $timeVar $dateVar

# Returned from PHP
passTime=$1
echo "$passTime"

passDate=$2
echo "$passDate"

PHP

<?php

setlocale(LC_CTYPE, "en_US.UTF-8");
echo "PHP called.\n";
$logTime = $argv[1]."\n";
//    echo $logTime;
$logDate = $argv[2]."\n";
//    echo $logDate;

$passTime = escapeshellarg('Time: $logTime'); 
$passDate = escapeshellarg('Date: '.$logDate);

?>

Running the shell, the only echo that it get back is 'PHP called.' As you can see, I've tried two different syntaxes in the last two php variables. Neither seem to work. Is there a better function for this? Or, am I just using the wrong syntax?

Thanks in advance for any suggestions

LOlliffe
  • 1,647
  • 5
  • 22
  • 43
  • your PHP code doesn't output anything EXCEPT your one `php called` echo. And your `$logTime` line is incorrect. `'`-quoted strings in PHP do NOT interpolate variables. your `$passTime` will literally contain `$`, `l`, `o`, `g`, etc... – Marc B Sep 02 '14 at 20:22
  • I originally found the example for `$passDate` and that looked correct to me, but I agree about the `'`-quoted string. I only tried it for completeness, because it was a "working example" that I found, although it obviously didn't work for me. I'm not really trying to get any output, per se, from the PHP, except for passing a string out of the PHP back to the encompassing shell script. – LOlliffe Sep 02 '14 at 20:37

2 Answers2

2

In the PHP script, you'll have to echo anything that you want the shell script to get as a "return value". Keep in mind though that you're not really returning anything. You're printing to stdout. Then you have to capture that stdOut into a variable in the shell script using someVariable=$(ExecuteScriptThatOutputsStdOut).

Here is a similar question/answer: redirect command output into variable and standard output in ksh

Here is your code modified to get/print the output from the PHP script. There are some other oddities you'll likely want to change, though that's a separate issue. (the linefeeds at the end of the assignments in the php, escaping shell args with hardcoded labels in them.)

If you want the date and time separately, you'll either have to call two separate php scripts, or, in the shell script, you'll have to parse the output from the PHP script and separate the result into the two variables.

Shell Script

#!/bin/sh

# set a date and time variables, in the form of  YYYY-MM-DD and HH:MM:SS
dateVar=`date +%Y-%m-%d`
timeVar=`date +%H_%M`

# Launching PHP
runPHP=`which php`
phpResult=$($runPHP calledPHP.php $timeVar $dateVar)

# Returned from PHP
echo "The php result is: $phpResult"

PHP

<?php

setlocale(LC_CTYPE, "en_US.UTF-8");
//echo "PHP called.\n";
$logTime = $argv[1]."\n";
//    echo $logTime;
$logDate = $argv[2]."\n";
//    echo $logDate;

$passTime = escapeshellarg("Time: $logTime");
$passDate = escapeshellarg("Date: $logDate");

echo "$passTime, $passDate";
?>
Community
  • 1
  • 1
Evan de la Cruz
  • 1,966
  • 1
  • 13
  • 17
  • Thanks so much for the help. I wasn't at all understanding how the examples that I was finding were working. I didn't get how PHP was passing out arguments to be fed into the $1 and $2 variables. Your example makes a lot more sense. For anyone else who may be helped by this example, the final shell echo, showing the variable is: `The php result is: 'Time: 17_36', 'Date: 2014-09-02'`, so if you just wanted the raw time or date, you'd need to further process it, but now at least you know how to get some data to work with. – LOlliffe Sep 03 '14 at 00:54
1

When a shell script invokes an external program, the only thing it can capture is the program's output. The program's state (environment, variables, etc) all disappear when the program's process exits.

You will have to arrange to have your PHP script emit data to stdout (i.e. echo it) and the shell script will parse the output (usually with the read command)

glenn jackman
  • 238,783
  • 38
  • 220
  • 352