Running a bash script in PHP
Hello all. I am trying to run a bash script in php, but I am having trouble with the exec. I have all my permissions at 777 for now so I could see if that was it. I am running windows, but my code is actually on a Linux site -_-. Here is my code. I have tried changing the file name from Project to Project.sh, I had it like this earlier
exec("/home/knf0011/public_html/Project.sh); exec('/home/knf0011/public_html/Project.sh'); exec('./home/knf0011/public_html/Project.sh'); Any help would be appreciated. PHP ==================== if($e=="Sample.cpp") { exec("/home/knf0011/public_html/Project.sh, &$output"); echo $output . "<BR>"; } BASH ===================== cat Sample.cpp > temp cat temp | while read line; do sed -e '1i\ ' temp | sed '/*/s/\// /g;s/\*/ /g;s/!/ /g;s/#line 1/ /g' > Sample done cat Sample | while read line; do sed '/./=' Sample | sed '/./N; s/\n/ /' > Lines done rm -rf temp cat Sample.cpp > temp cat temp | while read line; do sed -e '/./{H;$!d;}' -e 'x;/\/\*\!/!d;' | sed 'n' > RoutinePar done rm -rf temp cat Sample.cpp > temp cat temp | while read line; do sed -e '1i\ ' temp | sed -n '/\/\/\!/,/\/\*\!/p' | sed 'N;$!P;$!D;$d' > Header done cat Header | while read line; do sed 's/\/\/\!/ /g' Header > HeaderL cat HeaderL > Header rm -rf HeaderL done rm -rf temp cat Sample.cpp > temp cat RoutinePar | while read line; do sed -e '1i\ ' RoutinePar | sed -n '/\/\*/{n;p;}' | sed -ne 's/^.*\*\(.\)/\1/p' > Routine done cat RoutinePar | while read line; do awk 'BEGIN{RS=""; ORS="\n\n"}; NR!=2' RoutinePar > Routine0 done cat Routine0 | while read line; do awk 'BEGIN{RS=""; ORS="\n\n"}; NR!=2' Routine0 > Routine1 done cat Routine1 | while read line; do sed -e '1i\ ' Routine1 | sed -n '/\/\*\!/,/\!\*\//p' | sed 'N;$!P;$!D;$d' > Routine1Header done cat Routine1Header | while read line; do sed '/*/s/\// /g;s/\*/ /g;s/!/ /g;s/#line 1/ /g' > Routine1Header done cat Routine1 | while read line; do sed -n '/#line 1/,/\/\/ INI /p' | sed '/*/s/\// /g;s/\*/ /g;s/!/ /g;s/#line 1/ /g' > Routine1 done cat Routine1 | while read line; do sed '/./=' Routine1 | sed '/./N; s/\n/ /' > Routine1Lines done cat Routine0 | while read line; do awk 'BEGIN{RS=""; ORS="\n\n"}; NR!=1' Routine0 > Routine3 done cat Routine3 | while read line; do sed -e '1i\ ' Routine3 | sed -n '/\/\*\!/,/\!\*\//p' | sed 'N;$!P;$!D;$d' > Routine3Header done cat Routine3Header | while read line; do sed '/*/s/\// /g;s/\*/ /g;s/!/ /g;s/#line 1/ /g' > Routine3Header done cat Routine3 | while read line; do sed -n '/#line 1/,/\/\/ INI /p' | sed '/*/s/\// /g;s/\*/ /g;s/!/ /g;s/#line 1/ /g' > Routine3 done cat Routine3 | while read line; do sed '/./=' Routine3 | sed '/./N; s/\n/ /' > Routine3Lines done cat RoutinePar | while read line; do awk 'BEGIN{RS=""; ORS="\n\n"}; NR!=3' RoutinePar > RoutineX done cat RoutineX | while read line; do awk 'BEGIN{RS=""; ORS="\n\n"}; NR!=1' RoutineX > Routine2 done cat Routine2 | while read line; do sed -e '1i\ ' Routine2 | sed -n '/\/\*\!/,/\!\*\//p' | sed 'N;$!P;$!D;$d' > Routine2Header done cat Routine2Header | while read line; do sed '/*/s/\// /g;s/\*/ /g;s/!/ /g;s/#line 1/ /g' > Routine2Header done cat Routine2 | while read line; do sed -n '/#line 1/,/\/\/ INI /p' | sed '/*/s/\// /g;s/\*/ /g;s/!/ /g;s/#line 1/ /g' > Routine2 done cat Routine2 | while read line; do sed '/./=' Routine2 | sed '/./N; s/\n/ /' > Routine2Lines done |
try:
exec("/home/knf0011/public_html/Project.sh", &$output); That is close the quote after Project.sh and then have your $output as the second argument to exec. |
That didnt work either....
|
Well I guess that you are going to have to provide more information. What actually happens? Do you get an error? Can you run a built in command from PHP?, Can you run a simpler script from PHP? Can you run the script from the shell?
|
nothing happens. The bash file is supposed to split up the text into different pieces and save them out. I actually have it set to echo whatever it does, but it just echos a black out.
|
What about the other questions I asked? Specifically, can you run the script from the shell? And can you run a built in command (that's a shell command such as ls)?
|
what you are trying to do in that bash script can be done in PHP itself.
|
Yes, the file runs in shell script by itself. If you can't tell with the bash, I'm taking a part a text file and saving off pieces of it. I am not good with php, so I wrote that part in bash.
|
Assuming that you have tried my other suggestions and drawn a blank, is the code being executed? The exec is wrapped in an if. Add echo statements to ensure that the code is being run.
Code:
echo $e; |
I did this earlier
echo $e; if($e=="Sample.cpp") { echo Sample.cpp; exec("/home/knf0011/public_html/Project.sh, &$output"); echo $output . "<BR>"; } |
Quote:
Please of you want help try to be a little less terse. |
you can also try the following functions to help out...
file_exists() is_executable() Code:
if file_exists("/home/knf0011/public_html/Project.sh") |
try and echo the result from getcwd()
|
Quote:
Thank you all for your help. I didn't get it working, but I did something else instead for the project I did. |
Quote:
|
All times are GMT -5. The time now is 11:45 PM. |