bash commands using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
prad87
Forum Newbie
Posts: 3
Joined: Tue Feb 09, 2010 1:44 pm

bash commands using php

Post by prad87 »

i m trying to run multiple bash commands using php...
something like this " echo '[start]' ; cd /dir; ls; echo '[end]' "
now the problem is i want to run this bunch of commands on different hosts.
the {start] and [end] strings are mainly for gettin the output of bash shell.

the problem is for different hosts i m getting different output i.e. not the correct list of files in that dir

to print out the output i m using this method
start_time = time()
$max_time = 1;
while(((time()-$start_time) < $max_time)) {
$line = fgets($shell);

if(preg_match('/\[start/',$line)) {
$start = true;
}elseif(preg_match('/\[end\]/',$line)) {
return $output;
}elseif($start){
$output = $output.$line;
}
}


i know this is not the best method of gettin the output....can you please suggest other ways?...or help me modify this method?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: bash commands using php

Post by requinix »

Try chdir+exec.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: bash commands using php

Post by pickle »

If you're trying to get a list of files in a directory, use PHP's glob() functionality. Using BASH is fraught with disaster, as some hosts might not be using BASH - there's SH, KSH, and a bunch of other possible shells. Doing as much in PHP is best, because PHP takes care of the translation.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: bash commands using php

Post by requinix »

pickle wrote:If you're trying to get a list of files in a directory, use PHP's glob() functionality. Using BASH is fraught with disaster, as some hosts might not be using BASH - there's SH, KSH, and a bunch of other possible shells. Doing as much in PHP is best, because PHP takes care of the translation.
Building off of that, I was going to suggest:

Make your own ls function using PHP. It'll be the best cross-platform solution you can find. glob+stat will get you 95% of the way there.
Post Reply