I have a shell script that is created inside a php script (given full permissions). When I try to run the shell script from Terminal, I get no errors, but the script does not run (none of the commands execute). Although, once I copy the contents of the shell script, paste them into a new file in XCode, and overwrite the old shell script, it runs properly.
Any suggestions? I’ve been trying to figure this out for a very long time now with no progress.
I’m assuming that there is an issue with writing the shell script from the 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’s an example of a 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
Change all the
\rin yourfwritestatements to\nand move them to the end (or add a finalfwrite($script, "\n");at the end right before you close the file (I don’t see afclose(), by the way).or
Shell scripts should have newlines for line endings rather than carriage returns.