【发布时间】:2011-06-25 16:52:26
【问题描述】:
我有一个在 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
【问题讨论】:
-
写入文件的权限还是777吗?
-
我对那些
\r有点怀疑。 -
是的,权限仍然是 777。
-
试试
\n。您可以编辑您的答案以显示生成的脚本的示例吗?请使用@Dennis,以便用户收到回复时自动收到通知 -
@Dennis 对此感到抱歉 - 刚刚编辑了我的帖子并添加了 php 脚本创建的示例 shell 脚本