Reputation: 43
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
Upvotes: 3
Views: 1195
Reputation: 360325
Change all the \r
in your fwrite
statements to \n
and move them to the end (or add a final fwrite($script, "\n");
at the end right before you close the file (I don't see a fclose()
, by the way).
fwrite($script,"#!/bin/sh" . "\n");
...
fwrite($script, $modPath . "/mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".IR" . "\n");
...
fclose($script);
or
fwrite($script,"#!/bin/sh");
...
fwrite($script, "\n" . $modPath . "/mod -o " . $irPath . "/codes/" . $noT . " " . $fileName . ".IR" . "\n");
...
fwrite($script, "\n");
fclose($script);
Shell scripts should have newlines for line endings rather than carriage returns.
Upvotes: 4