LOlliffe
LOlliffe

Reputation: 1675

Passing arguments back and forth between PHP and shell

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

Upvotes: 0

Views: 1198

Answers (2)

Evan de la Cruz
Evan de la Cruz

Reputation: 1966

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";
?>

Upvotes: 2

glenn jackman
glenn jackman

Reputation: 246799

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)

Upvotes: 1

Related Questions