LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Running a bash script in PHP (https://www.linuxquestions.org/questions/programming-9/running-a-bash-script-in-php-689018/)

Nerdstock2005 12-07-2008 09:17 PM

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

graemef 12-07-2008 10:48 PM

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.

Nerdstock2005 12-07-2008 11:57 PM

That didnt work either....

graemef 12-08-2008 12:37 AM

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?

Nerdstock2005 12-08-2008 12:59 AM

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.

graemef 12-08-2008 01:46 AM

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)?

ghostdog74 12-08-2008 02:24 AM

what you are trying to do in that bash script can be done in PHP itself.

Nerdstock2005 12-08-2008 02:31 AM

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.

graemef 12-08-2008 04:41 AM

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;
if($e=="Sample.cpp")
{
echo "About to call exec";
exec("/home/knf0011/public_html/Project.sh", $output);
echo $output . "<BR>";

}


Nerdstock2005 12-08-2008 05:04 AM

I did this earlier

echo $e;
if($e=="Sample.cpp")
{
echo Sample.cpp;
exec("/home/knf0011/public_html/Project.sh, &$output");
echo $output . "<BR>";

}

graemef 12-08-2008 05:28 AM

Quote:

Originally Posted by Nerdstock2005 (Post 3367930)
I did this earlier

and...

Please of you want help try to be a little less terse.

graemef 12-08-2008 05:34 AM

you can also try the following functions to help out...

file_exists()
is_executable()
Code:

if file_exists("/home/knf0011/public_html/Project.sh")
  echo "File exists"
else
  echo "Cannot find the file";
if is_executable("/home/knf0011/public_html/Project.sh")
  echo "and is executable"
else
  echo "Cannot execute the file


graemef 12-08-2008 05:37 AM

try and echo the result from getcwd()

Nerdstock2005 12-09-2008 02:32 AM

Quote:

Originally Posted by graemef (Post 3367950)
and...

Please of you want help try to be a little less terse.

I wasn't being terse, I posted what I tried earlier...

Thank you all for your help. I didn't get it working, but I did something else instead for the project I did.

graemef 12-09-2008 03:28 PM

Quote:

Originally Posted by Nerdstock2005 (Post 3368842)
I wasn't being terse, I posted what I tried earlier...

As well as posting what you tried, it is just as important (if not more so) to tell us what the result was, then it is possible for us to give you meaningful feedback and advice.


All times are GMT -5. The time now is 11:45 PM.