By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

executing multiple shell commands via one exec()-call

P: n/a
I wrote a PHP shell script under Linux which puts all existing
[E]PS-Files within a directory into a list and should then start a
single Ghostview window for each file. Sounds simple, but it's not:

The following code provides for about half of the functionality I
wanted:

==============code starts
$bla = array();
foreach ($filelist as $file)
{
$bla[] = fopen("./trash/".$file.".tmp", "w");
exec ("gv ".$file." &", $bla);
}
===============code ends

Here, all files are opened one after another. The next file opens only
after closing the current one. Actually I don't know whether the
redirection of the output towards .tmp-files complies with the
intended purpose of the redirection. The PHP manual states that when
starting programs in background, the output would have to be
redirected to a stream or file, otherwise PHP would continue to run
until the respective program is terminated (which I really wouldn't
care about at all, if it did so...).

Then I thought about writing the line I would type manually for
opening all ps-files ('gv bla1.ps & ; gv bla2.ps&; ...') into a string
and then executing that string:

===============code starts
$command="";
$bla = fopen("./trash/tmp", "w");
foreach ($filelist as $i => $file)
{
if ($i < count($filelist) - 1)
{
$command = $command."gv $file & ; ";
}
else
{
$command = $command."gv $file &";
}
}
exec ($command, $bla);
===============code ends

sadly I get an error message from the shell:
================ error message starts
sh: -c: line 1: syntax error near unexpected token `;'
sh: -c: line 1: `gv uges_Boden.ps & ; gv ux_Boden.ps & ; gv
uy_Boden.ps &'
================ error message ends

where *_Boden.ps are the files I wanted to open. Confusing me
perfectly, when I copied the command of the error message's second
line and pressed <return>, the thing I wanted to achieve happened: All
three files opened in their own Ghostview window.

Can anybody explain to me, what I got wrong here?

Thanks a lot!
Falk
Jul 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I found out by the help of a work mate, the solution is to redirect
the standard streams of the shell (in/out/error) to /dev/null within
the system call:

==============code starts
foreach ($filelist as $file)
{
$bla = "gv ".$file;
exec ($bla." >/dev/null 1>/dev/null 2>/dev/null &);
}
===============code ends
That's all it takes. Works perfectly.

Greetz
Falk
Jul 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.