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?
bash commands using php
Moderator: General Moderators
Re: bash commands using php
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.
Re: bash commands using php
Building off of that, I was going to suggest: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.
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.