LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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-19-2007, 11:56 PM   #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, 01:55 AM   #2
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

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

"`" it is a backtick!

Last edited by j-ray; 11-20-2007 at 02:36 AM.
 
Old 11-20-2007, 05:13 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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, 05:47 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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, 06:10 AM   #5
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
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, 04: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, 06: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, 06:19 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 08:14 PM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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, 08:42 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 09: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 09:16 PM. Reason: corrected function name...
 
Old 11-21-2007, 04: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



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

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

All times are GMT -5. The time now is 04:57 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