Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi, i need to perform the following function
./ntlogin.sh data.out > new.bat
this command runs perfectly on the prompt.but i wish to execute it inside another program.i.e. on executing a file say run.cgi i wish that the above code is executed.simply keeping it as it is inside run.cgi is not working.pleaz help me... thank you

Replies are listed 'Best First'.
Re: execution of cmmand inside a program
by bm (Hermit) on Jul 11, 2003 at 09:20 UTC
    my $cmd = './ntlogin.sh data.out > new.bat'; system($cmd) == 0 or die "system cmd '$cmd' failed: $?";

    but as mentioned you should also check out perldoc -f system, especially with regard to detecting if your command executed correctly.
      To head off the next question (in the event that the command fails), check and make sure the web server user has permissions to run the command and verify that the command is in the web server user's path. Or, better yet, use full paths.
Re: execution of cmmand inside a program
by antirice (Priest) on Jul 11, 2003 at 07:51 UTC

    perldoc -f system

    Other suggested reading: How to RTFM. ;-)

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      hi, you have been helping me since the very beginning.from the time i took ur advice and started architecting my own project myself u have guided me continuously.i THANK YOU very much...and wish that GOD will always help u in some way whenever u ask for it. thank you once again
Re: execution of cmmand inside a program
by blue_cowdawg (Monsignor) on Jul 11, 2003 at 15:14 UTC
    close STDOUT or die $!;
    close STDERR or die $!;
    
    open STDOUT, "> new.bat" or die $!;
    exec "./ntlogin.sh" or die $!;
    

    That's one way of doing this.


    Peter L. BergholdBrewer of Belgian Ales
    Peter@Berghold.Netwww.berghold.net
    Unix Professional
Re: execution of cmmand inside a program
by blue_cowdawg (Monsignor) on Jul 11, 2003 at 15:24 UTC

    From the variation on a theme department

    spawn("./ntlogin.sh",undef,"data.out"); exit(0); # # spawn($cmd[,$env_vars,[,$stdout_log[,$stderr_log]]]) # $cmd = the command to execute # $env_vars = anonymous hash containing special environmenal # values (eg VAR => VALUE) # $stdout_log = a path to a log file for stdout # $stderr_log = a path to a log file for stderr sub spawn { my ($cmd_to_exec,$env_vars,$stdout_log,$stderr_log)=@_; my $pid=fork(); #Fork off! if ($pid > 0 ) { # This is the parent process return $pid; } elsif ( $pid == 0 ) { # this is the child if ($env_vars) { # # We have special environmental vars to pass foreach my $key(keys %$env_vars) { $ENV{$key}=$env_vars->{$key}; } } # # Next we want to close STDOUT and STDERR close STDOUT; close STDERR; # # Check to see if we have a filename passed if ($stdout_log) { # # If there is no stderr_log if (! $stderr_log) { $stderr_log=">&STDOUT"; } else { $stderr_log="> " . $stderr_log; } $stdout_log="> $stdout_log"; open STDOUT,$stdout_log or die "$stdout_log:$!"; open STDERR,$stderr_log or die "$stderr_log:$!"; } # # Finally we exec the command exec $cmd_to_exec or die "$cmd_to_exec: $!"; } else { die "Could not fork! :$!"; } }

    That's code hot off pressses. That's a sniglet from some code I wrote for an startup script to replace some crippleware one of our vendors supplied with their product.


    Peter L. BergholdBrewer of Belgian Ales
    Peter@Berghold.Netwww.berghold.net
    Unix Professional