Professional Web Applications Themes

executing multiple shell commands via one exec()-call - PHP Development

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 ...

  1. #1

    Default executing multiple shell commands via one exec()-call

    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
    Falk Schneider Guest

  2. #2

    Default Re: executing multiple shell commands via one exec()-call

    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
    Falk Schneider Guest

Similar Threads

  1. Replies: 0
    Last Post: October 22nd, 01:20 AM
  2. Replies: 2
    Last Post: October 18th, 03:16 PM
  3. Replies: 1
    Last Post: October 4th, 04:13 AM
  4. [PHP] executing shell commands.
    By Samir Sethi in forum PHP Development
    Replies: 7
    Last Post: September 11th, 07:37 AM
  5. Replies: 0
    Last Post: September 10th, 10:12 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139