Page 1 of 1

bash commands using php

Posted: Wed Apr 21, 2010 2:44 pm
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?

Re: bash commands using php

Posted: Wed Apr 21, 2010 3:45 pm
by requinix
Try chdir+exec.

Re: bash commands using php

Posted: Wed Apr 21, 2010 5:33 pm
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.

Re: bash commands using php

Posted: Wed Apr 21, 2010 7:46 pm
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.