LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2008, 09:17 PM   #1
Nerdstock2005
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Rep: Reputation: 0
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
 
Old 12-07-2008, 10:48 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 12-07-2008, 11:57 PM   #3
Nerdstock2005
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Original Poster
Rep: Reputation: 0
That didnt work either....
 
Old 12-08-2008, 12:37 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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?
 
Old 12-08-2008, 12:59 AM   #5
Nerdstock2005
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 12-08-2008, 01:46 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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)?
 
Old 12-08-2008, 02:24 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
what you are trying to do in that bash script can be done in PHP itself.
 
Old 12-08-2008, 02:31 AM   #8
Nerdstock2005
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 12-08-2008, 04:41 AM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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>";

}

Last edited by graemef; 12-08-2008 at 05:25 AM.
 
Old 12-08-2008, 05:04 AM   #10
Nerdstock2005
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Original Poster
Rep: Reputation: 0
I did this earlier

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

}
 
Old 12-08-2008, 05:28 AM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by Nerdstock2005 View Post
I did this earlier
and...

Please of you want help try to be a little less terse.
 
Old 12-08-2008, 05:34 AM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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
 
Old 12-08-2008, 05:37 AM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
try and echo the result from getcwd()
 
Old 12-09-2008, 02:32 AM   #14
Nerdstock2005
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by graemef View Post
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.
 
Old 12-09-2008, 03:28 PM   #15
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by Nerdstock2005 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please translate this PHP script to BASH protorox Programming 8 03-28-2014 12:09 PM
Is 'nice' inherited to child processes? e.g. bash script/php script that calls MySQL SirTristan Linux - Newbie 1 12-04-2008 12:57 AM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
Running bash commands from php. rookiepaul Linux - General 3 03-22-2006 02:22 AM
running su from a bash script caminoix Programming 7 12-28-2005 03:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration