0

I am trying to execute a bash script from within a php script.

The php script is:

    <?php

    $clientName = $_POST['clientName'];
    $startDate = $_POST['startDate'];
    $endDate = $_POST['endDate'];
    $mode = $_POST['mode'];

    echo("Current working directory = " . getcwd());
    echo("Client Name = " . $clientName . "<br/>\n");
    echo("Start date = " . $startDate . "<br/>\n");
    echo("End date = " . $endDate . "<br/>\n");
    echo("Mode = " . $mode . "<br/>\n");

    $cmd = "/webroot/argRepeater.bash escapeshellarg($clientName) escapeshellarg($startDate) escapeshellarg($endDate) escapeshellarg($mode)";

echo("Command = " . $cmd . "<br/>\n");
var_dump($cmd);

    exec("/bin/bash ./argRepeater.bash escapeshellarg($clientName) escapeshellarg($startDate) escapeshellarg($endDate) escapeshellarg($mode)", $output, $output2);

    echo("Output array = " . print_r($output) . "<br/>\n");
    echo("Output = " . $output2 . "<br/>\n");

    ?>

The above php script takes arguments from an html form. The bash script argRepeater.bash only repeats whatever arguments were given to it. The output is as follows:

 Current working directory = /home/content/31/10199331/htmlClient Name = yum
    Start date = 2013-05-14
    End date = 2013-05-24
    Mode = fir
   Command = ./argRepeater.bash escapeshellarg(yum) escapeshellarg(2013-05-14) escapeshellarg(2013-05-24) escapeshellarg(fir)
string(128) "./argRepeater.bash escapeshellarg(yum) escapeshellarg(2013-05-14) escapeshellarg(2013-05-24) escapeshellarg(fir)" Array ( ) Output array = 1
    Output = 1  

My questions:
1. What more needs to be done to ensure argRepeater gets executed?
2. How do I display argRepeater's outputs on the webpage?

1 Answer 1

1

1. What more needs to be done to ensure argRepeater gets executed?

Well it needs all that it needs. Troubleshoot your needs if you are not so sure if it worked out or not. Output = 1 signals it did work in the sense that something was executed.

2. How do I display argRepeater's outputs on the webpage?

You already do it:

echo("Output array = " . print_r($output) . "<br/>\n");

so as well, you ask about something that already is solved by your own code.

So probably the problem is just that you are uncertain if you understand your code or not? If so, just double check every statement you write with the manual.

Sign up to request clarification or add additional context in comments.

6 Comments

Correct me if I am wrong, but output2 will contain the exit status of the command. since bash returns non-zero on unsuccessful completion, I think the bash script did not execute properly. Am I right?
That depends on the bash script. each bash script can use it's own return codes, and 1 must not signal failure per-se. Which means you might be right, yes. However it also can show that something did work (also I didn't say you should not further trouble-shoot this). tldp.org/LDP/abs/html/exit-status.html
I did have written some good troubleshooting guide for shell commands from PHP, I will dig them up, they might be helpful here as well if I see that right. Edit: Please see my answer on "php shell exec acting different to running command directly" for an effective debugging technique.
To the above bash script, I added echo "Hello World" >> hello.txt. there is no hello.txt in the directory. These files are hosted on godaddy web hosting. could that be a problem?
Put the command into a variable first. Output the variable (without executing the command). Does it contain your expected command? (this is independent to your hoster)
|

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.