2011-02-11 96 views
3

我有一個在php腳本(給定完全權限)內創建的shell腳本。當我嘗試從終端運行shell腳本時,我得不到任何錯誤,但腳本不運行(沒有任何命令執行)。儘管一旦我複製了shell腳本的內容,將它們粘貼到XCode中的一個新文件中,並覆蓋舊的shell腳本,它就會正常運行。無法運行具有完全權限的shell腳本(UNIX)

有什麼建議嗎?我一直試圖找出很長一段時間,現在沒有進展。

我假設從PHP腳本編寫shell腳本時存在問題,因爲它在XCode或文本編輯器中編寫時工作。

這裏是PHP腳本編寫shell腳本:

<code> 
    $filePath = "/Applications/MAMP/htdocs/php/Batch/modulator/Release23/Library/irShell.sh"; 
    $script = fopen($filePath, 'w+'); 
    chmod($filePath, 0777); 
    fwrite($script,"#!/bin/sh"); 
    $irPath = "/Applications/MAMP/htdocs/php/Batch/modulator/Release23/Library"; //path to .ir files 
    $modPath = "/Applications/MAMP/htdocs/php/Batch/modulator"; 

    if($dir = opendir($irPath)){   
     while(($file = readdir($dir)) !== false){ 
      $posA = strpos($file, ".IR"); 
      $posB = strpos($file, ".ir"); 
      $posC = strpos($file, ".Ir"); 
      if ($posA == true){ 
       $fileName = trim($file, ".IR"); 
       $noT = substr_replace($fileName, "", 0, 1); 
       echo "$noT\n"; 
       fwrite($script, "\r" . $modPath . "/mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".IR"); 
      } 
      else if ($posB == true){ 
       $fileName = trim($file, ".ir"); 
       $noT = substr_replace($fileName, "", 0, 1); 
       echo "$noT\n"; 
       fwrite($script, "\r" . $modPath . "/mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".ir"); 
      } 
      else if ($posC == true){ 
       $fileName = trim($file, ".Ir"); 
       $noT = substr_replace($fileName, "", 0, 1); 
       echo "$noT\n"; 
       fwrite($script, "\r" . $modPath . "./mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".Ir"); 
      } 
     } 
    } 
</code> 

下面是由這個PHP生成的shell腳本的例子:

#!/bin/sh

/Applications/MAMP/htdocs/php/Batch/modulator/mod -o /Applications/MAMP/htdocs/php/Batch/modulator/Release23/Library/codes/1294 T1294.ir 
/Applications/MAMP/htdocs/php/Batch/modulator/mod -o /Applications/MAMP/htdocs/php/Batch/modulator/Release23/Library/codes/1295 T1295.ir 
/Applications/MAMP/htdocs/php/Batch/modulator/mod -o /Applications/MAMP/htdocs/php/Batch/modulator/Release23/Library/codes/1296 T1296.ir 

+0

寫入文件的權限仍爲777嗎? – chrisaycock 2011-02-11 17:03:59

+0

我對那些`\ r`有點懷疑。 – 2011-02-11 17:16:11

回答

4

改變所有的\r在您的fwrite聲明到\n並將它們移動到最後(或在關閉文件之前在最後添加最終的fwrite($script, "\n");(我沒有看到順便說一句,)。

fwrite($script,"#!/bin/sh" . "\n"); 
... 
fwrite($script, $modPath . "/mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".IR" . "\n"); 
... 
fclose($script); 

fwrite($script,"#!/bin/sh"); 
... 
fwrite($script, "\n" . $modPath . "/mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".IR" . "\n"); 
... 
fwrite($script, "\n"); 
fclose($script); 

Shell腳本應該有行結束,而不是回車換行。