无法以完全权限运行shell脚本(UNIX)

时间:2011-02-11 17:01:01

标签: php bash shell unix

我有一个在php脚本中创建的shell脚本(给定完全权限)。当我尝试从终端运行shell脚本时,我没有错误,但脚本没有运行(没有任何命令执行)。虽然,一旦我复制了shell脚本的内容,将它们粘贴到XCode中的新文件中,并覆盖旧的shell脚本,它就可以正常运行。

有什么建议吗?我一直试图解决这个问题很长一段时间没有进展。

我假设从php脚本编写shell脚本存在问题,因为它在XCode或文本编辑器中编写时有效。

这是编写shell脚本的php脚本:

<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

/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

1 个答案:

答案 0 :(得分:4)

\r语句中的所有fwrite更改为\n并将其移至最后(或在关闭文件之前在末尾添加最终fwrite($script, "\n"); (顺便说一下,我没有看到fclose()

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脚本应该有行结尾的换行符而不是回车符。