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

Hi, I have a question here. I am trying to invoke a command using my script. Please see the code below.
$Src_Cmd = "srvrmgr – g $GATEWAY –e $ENTERPRISE_SERVER –s $USER –p $PA +SSWD –c Run Task for comp $SYSTEM_NAME processName=\"UDA Batch Deplo +yment\", "RowId=$row_id"; #Subroutine to invoke the command sub Command_exe { ($COMMAND) = @_; print "Command to execute=$COMMAND\n"; $ret = system($COMMAND); if($ret == 0) { print "command executed successfully\n"; return 0; } else { print "Command Failed\n";return 1; } return 0; } #main------- $ret = Command_exe("$Src_Cmd"); # invoke the command line. print "ret code=$ret\n";
When i run this, it throws error. Do i need to write like this:
$Src_Cmd = "srvrmgr","– g","$GATEWAY","–e","$ENTERPRISE_SERVER","–s"," +$USER","–p","$PASSWD","–c",Run Task for comp $SYSTEM_NAME processName +=\"UDA Batch Deployment\", "RowId=$row_id";
Please suggest how to use the command? Cheers!! Ethen

Replies are listed 'Best First'.
Re: How to run a command from Perl
by Corion (Patriarch) on Jan 18, 2008 at 09:24 UTC

    What error does it "throw"?

    Without you providing the error message, it's kinda hard to guess, but I'm trying:

    The string:

    $Src_Cmd = "srvrmgr – g $GATEWAY –e $ENTERPRISE_SERVER –s $USER –p $PA +SSWD –c Run Task for comp $SYSTEM_NAME processName=\"UDA Batch Deplo +yment\", "RowId=$row_id";

    has unbalanced/unquoted double quotes. Perl even tells you that there is an error in the first line. I suggest using the qq operator instead:

    $Src_Cmd = qq{srvrmgr – g $GATEWAY –e $ENTERPRISE_SERVER –s $USER –p $ +PASSWD –c Run Task for comp $SYSTEM_NAME processName="UDA Batch Depl +oyment", "RowId=$row_id"};
Re: How to run a command from Perl
by ysth (Canon) on Jan 18, 2008 at 09:21 UTC
      Hi, This is the error I got:
      C:\MIG>perl label.pl 2 ret code=0 Command to execute=srvrmgr û g ûe ûs ûp ûc Run Task for comp proc +essName="U DA Batch Deployment", "RowId=" 'srvrmgr' is not recognized as an internal or external command, operable program or batch file. Command Failed ret code=1 ret code=0 Command to execute=` ûg ûe ûs ûp ûc Run Task for comp processName +="UDA Batc h Deployment", "RowId=" '`' is not recognized as an internal or external command, operable program or batch file. Command Failed ret code=1
      Thanks Ethen
        So did you read that message? It's fairly obvious: The comamnds srvmgr and ` can't be found.

        The first one is probably not found in PATH, so you have to adjust the environment variables accordingly.

        The second is clearly an error in the way you try to call some program.

        Provide the full path to svrmgr, e.g. on windows 'c:\\full\\path\\to\\svrmgr' or VMS dka1:[full.path.to]svrmgr.

Re: How to run a command from Perl
by zentara (Cardinal) on Jan 18, 2008 at 14:35 UTC
    I've seen some tricky commands that act like that. You may have to spend some time experimenting on how to put the options into an array paired up. For instance:
    my $command = "srver mgr"; my @options = ("-g $gateway", "-e $enterprise_server", etc, etc); #then system($command, @options);
    The pairing needed can sometimes be needed in a specific order too. I had to spend an hour once, experimenting, because some of the options needed pairing, yet other options didn't. Drove me crazy. :-)

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum