LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 11-20-2007, 12:56 AM   #1
creatorrr
Member
 
Registered: Nov 2007
Posts: 40

Rep: Reputation: 15
Execute bash script inside PHP


Hi All,

I want to be able to display the content of text file on website. For example, to see the output of the command "cat /somedir/somefile" on the webpage.

Regards
 
Old 11-20-2007, 02:55 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,441

Rep: Reputation: 103Reputation: 103
$text=`/bin/cat /somedir/somefile`;
echo $text;

"`" it is a backtick!

Last edited by j-ray; 11-20-2007 at 03:36 AM.
 
Old 11-20-2007, 06:13 AM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,193

Rep: Reputation: 538Reputation: 538Reputation: 538Reputation: 538Reputation: 538Reputation: 538
j-ray: that is pure bash, I don't know if that works.

creatorrr: check the exec command in PHP. there is an optional parameter which let you collect all the output of your bash command in an array.

so:

PHP Code:
exec ("/bin/cat myfile.txt", &$output); 
gives you the contents of the text file in the array $output.

jlinkels
 
Old 11-20-2007, 06:47 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,158

Rep: Reputation: 247Reputation: 247Reputation: 247
Hi

If all you want to do is read a file and display it, the readfile function is better, since the server doesn't have to start a shell on every page hit.
 
Old 11-20-2007, 07:10 AM   #5
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu
Posts: 1,441

Rep: Reputation: 103Reputation: 103
as far as i know the backticks operator works without invoking a shell but of course the readfile function could be used as well.
exec, passthru and backticks are functions to invoke/execute other scripts and programs so one has to be careful with dynamically posted commands here.
 
Old 11-20-2007, 05:38 PM   #6
creatorrr
Member
 
Registered: Nov 2007
Posts: 40

Original Poster
Rep: Reputation: 15
Thanks guys, all sugestions above kind of worked.

$text=`/bin/cat /somedir/somefile`;
echo $text;

and

readfile

gives me the text file unformatted, sort of cannot detect end of line, and exec ("/bin/cat myfile.txt", &$output); gives me an array. I am sure i am close to solution here, will try to manipulate the output, or printout the array and see how its going...

Thanks again
 
Old 11-20-2007, 07:07 PM   #7
creatorrr
Member
 
Registered: Nov 2007
Posts: 40

Original Poster
Rep: Reputation: 15
Guys could you please help me with this one, it must be something very simple but i am failing to fix to: Whatever i tried from the above mentioned solutions i am getting this output:

hello world hello world2

instead of

hello world
hello world2

Thank You
 
Old 11-20-2007, 07:19 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
If you need to ensure you cvt \n to <br> or <p>, try good old fopen/fread/fclose set and do your own char conversions.
 
Old 11-20-2007, 09:14 PM   #9
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,193

Rep: Reputation: 538Reputation: 538Reputation: 538Reputation: 538Reputation: 538Reputation: 538
OK, you get an array named $output.

PHP Code:
foreach ($output as $line){
  echo 
"$line <br>";

The code is untested as I write it, This is and excerpt from my real code:

PHP Code:
        $s_exec LOAD_SCMASTER " $sched_file $db_name";
        echo 
"exec string: $s_exec <br>";
        
exec ($s_exec, &$a_log);
                
$err =0;
                foreach (
$a_log as $log){
                        echo 
$log"<br>";
                        if (
strpos($log"rror") > 0){
                                
$err=1;
                        }
                }
                
?> 
The "problem" here (not a real problem, just straightforwardness) is that in bash CRLF translates to a new line on your screen, while in HTML you need a <br> tag to accomplish the same.

The backtick or read method work as well, you only must put some effort in translating CRLF (or just CR in the Linux world, or was it LF?) into <br>. It is the old tradeoff between quick coding and efficient code.

jlinkels
 
Old 11-20-2007, 09:42 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by creatorrr View Post
readfile

gives me the text file unformatted,
use fread to read line by line, then format as you wished
Code:
$f=fopen("filename","r");
while ( !feof($f) )
{
  $line=fread($f);
  echo "$line<br>";
}
 
Old 11-20-2007, 10:15 PM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
There is a php function that converts new lines to html breaks, I think that it is called nl2br()

Last edited by graemef; 11-20-2007 at 10:16 PM. Reason: corrected function name...
 
Old 11-21-2007, 05:32 PM   #12
creatorrr
Member
 
Registered: Nov 2007
Posts: 40

Original Poster
Rep: Reputation: 15
Thanks Guys,

Tried all of them, all works, the simplest solution seems to be nl2br().

Thanks for you help
 
  


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
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 10:10 PM
Can I execute a shell script from inside a php one. prabhatsoni Linux - Software 2 05-24-2006 06:40 AM
Execute a windows command from inside a c-cgi-script leamassiot General 51 02-09-2006 11:06 AM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 12:51 PM


All times are GMT -5. The time now is 07:21 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration