【问题标题】:Unable to run shell script with full permissions (UNIX)无法以完全权限运行 shell 脚本 (UNIX)
【发布时间】: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 脚本

标签: php bash shell unix


【解决方案1】:

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

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

【讨论】:

  • 非常感谢丹尼斯,就是这样
  • 回车/换行问题很常见,有一个命令可以解决它:dos2unix
猜你喜欢
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 2013-03-16
  • 2012-12-11
  • 2013-08-14
  • 2014-09-29
  • 2015-08-16
  • 1970-01-01
相关资源
最近更新 更多