in reply to Re^3: Failed System/Exec Call under Right Permission with CGI
in thread Failed System/Exec Call under Right Permission with CGI

McDarren,
Following your advice, I have this snippet.
my @args = ('perl', '/home/myname/public_html/somedir/cgi-bin/prn_to_file.pl',$param1, ,'>','/home/myname/public_html/somedir/results/output.txt'); system(@args) == 0 or die "Code does not work $!";
While it does prints/shows this text in the browser (which is not what I really want):
THIS IS YOUR INPUT TEXT: foo bar
But still, no file is created. Did I still miss anything?

Regards,
Edward

Replies are listed 'Best First'.
Re^5: Failed System/Exec Call under Right Permission with CGI
by McDarren (Abbot) on Jul 19, 2006 at 10:16 UTC
    I think you mis-understood my first point above. The whole idea of using the multi-arg form of system is to avoid shell processing. The (small) price you pay for this is that you can't do redirection or piping like you might using the single-arg form.

    In your code snippet above, you are effectively calling perl and passing it 4 arguments, the last three of which it won't know what to do with.

    If I understand correctly what you are trying to achieve, then I would suggest that you need to modify your prn_to_file.pl script so that it accepts two arguments. The first will be the text to be printed, and the second will be the name of the file it prints to. Then you make your system call as per my previous example.

    Cheers,
    Darren

      Dear McDarren,
      So sorry to have to comeback to you again McDarren. I'll try not to trouble you again after this....

      I've followed your suggestion. Now I have modified my prn_to_file.pl to take two arguments. It looks like this:
      #!/usr/bin/perl -w my $text = $ARGV[0]; my $OUTFILE_file_name = $ARGV[1]; # output file name open ( OUTFILE, '>', $OUTFILE_file_name ) or die "$0 : failed to open output file $OUTFILE_file_name : $!\n" +; print OUTFILE "THIS IS YOUR INPUT TEXT: $text\n"; close ( OUTFILE ); # close output file
      And the system call is as follows:
      my @args = ( # This is line 54 '/home/myname/public_html/MyTest/prn_to_file.pl', $param1, '/home/myname/public_html/MyTest/results/output.txt' ); system(@args) == 0 or die "Code does not work $!";
      But when I run it. It gives this as output in the browser:
      Testing System/Exec function with CGI This text _should_ be printed to the file : foo bar Software error: Code does not work Bad file descriptor at /home/myname/public_html/MyT +est/cgi-bin/test.cgi line 54. For help, please send mail to the webmaster (webmaster@cheers.com), gi +ving this error message and the time and date of the error.


      ---
      neversaint and everlastingly indebted.......
        "So sorry to have to comeback to you again McDarren. I'll try not to trouble you again after this.... "

        You should never feel like you need to apologise for asking advice. That's what SOPW is for :)

        Anyway, this is almost certainly a permissions problem. You need to ensure that the user the webserver runs as has appropriate permission to write to the directory involved.

        Here is an example that I've tested, and works (with appropriate permissions set):

        never.cgi

        #!/usr/bin/perl -w use strict; use CGI qw/:standard/; use Cwd; print header(); my $dir = getcwd; my $param1 = qq(hello, cruel world. A test; for \\\$neversaint.); my @args = ( "$dir/never1.pl", $param1, "$dir/test/never.out" ); system(@args) == 0 or die "Code does not work:$!"; exit;
        ..and the script that is called:
        #!/usr/bin/perl -w use strict; my $text = $ARGV[0]; my $outfile = $ARGV[1]; open OUT, ">", $outfile or die "$0 : failed to open output file $outfile:$!\n"; print OUT "THIS IS YOUR INPUT TEXT: $text\n"; close OUT; exit 0;
        and after loading the cgi script in my web browser:
        cat test/never.out THIS IS YOUR INPUT TEXT: hello, cruel world. A test; for \$neversaint +.

        Cheers,
        Darren