I have a shell script that is created inside a PHP script (with full permissions). When I try to start a script shell from Terminal, I get no errors, but the script does not start (none of the commands are executed). Although, after copying the contents of the shell script, paste them into a new file in Xcode and overwrite the old shell script, it works correctly.
Any suggestions? I tried to figure this out for a very long time without any success.
I assume that there is a problem with writing a shell script from a PHP script, since it works when written in Xcode or a text editor.
Here is the php script that writes the shell script:
<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>
And here is an example shell script that is generated by this php:
#!/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
code>