2013-05-04 219 views
0

我想從php腳本內執行bash腳本。從php執行bash腳本

PHP腳本是:

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

    ?> 

上面PHP腳本從HTML表單接受參數。 bash腳本argRepeater.bash只重複提供給它的任何參數。輸出如下:

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 

我的問題:
1.什麼更多的事情要做,以確保argRepeater被執行?
2.如何在網頁上顯示argRepeater的輸出?

回答

1

1.還需要做些什麼來確保argRepeater被執行?

那麼它需要它所需要的一切。如果您不確定它是否奏效,請排除您的需求。 Output = 1表示它在某事被執行的意義上確實工作。

2.如何在網頁上顯示argRepeater的輸出?

你已經做到這一點:

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

左右爲好,你問的東西,已經被自己的代碼來解決。

所以可能的問題是你不確定你是否理解你的代碼?如果是這樣,請仔細檢查您使用手冊編寫的每一條陳述。

+0

糾正我,如果我錯了,但'output2'將包含命令的退出狀態。因爲bash在不成功完成時返回非零值,所以我認爲bash腳本沒有正確執行。我對嗎? – Sriram 2013-05-04 20:04:25

+0

這取決於bash腳本。每個bash腳本都可以使用它自己的返回代碼,並且1本身不能表示失敗。這意味着你可能是對的,是的。然而它也可以表明某件事情有效(我也沒有說你不應該進一步麻煩 - 拍這個)。 http://tldp.org/LDP/abs/html/exit-status.html – hakre 2013-05-04 20:05:37

+0

我的PHP編寫了一些很好的shell命令故障排除指南,我會把它們挖出來,如果我看到它們可能會有所幫助那是正確的。 *編輯:*請參閱[我的答案](http://stackoverflow.com/a/14021630/367456)[*「php shell exec與直接運行命令不同」*](http://stackoverflow.com/ q/14021352/367456)以獲得有效的調試技術。 – hakre 2013-05-04 20:09:30