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

3
我有一个Shell脚本,是在PHP脚本中创建的(已授予完全权限)。当我尝试从终端运行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


写入文件的权限仍然是777吗? - chrisaycock
我对那些\r有点怀疑。 - Dennis Williamson
是的,权限仍然是777。 - SteveMack
尝试使用\n。您能否编辑您的答案,展示一下生成脚本的示例?请使用@Dennis,以便用户可以自动收到回复通知。 - Dennis Williamson
@Dennis 抱歉,刚刚编辑了我的帖子,并添加了一个由php脚本创建的示例shell脚本。 - SteveMack
1个回答

4
将所有\r替换为\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脚本应该使用换行符而不是回车作为行尾。


非常感谢,Dennis,就是这样。 - SteveMack
回车符/换行符问题非常普遍,因此有一个命令可以解决它:dos2unix - klang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接