Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Launching an external command from Perl in Linux

by wruehl (Acolyte)
on May 05, 2009 at 15:48 UTC ( #761969=perlquestion: print w/replies, xml ) Need Help??

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

I am attempting to call a function from within a perl script that will contact a monitoring program. I am getting compilation errors on this line of code.

 $testmessage = system(/usr/bin/postemsg -S tecserver -r(harmless) -m "(This is a test message)" hostname=(Trapeze) notify_action=email alias_notify=(pbkhk) (Event_Message_Class) (SOURCE));

I am getting the following as output:

Bareword found where operator expected at TDC-fabric-monitor.pl line +172, near "/usr/bin" (Missing operator before bin?) Unquoted string "tecserver" may clash with future reserved word at TDC +-fabric-monitor.pl line 172. Unquoted string "harmless" may clash with future reserved word at TDC- +fabric-monitor.pl line 172. Bareword found where operator expected at TDC-fabric-monitor.pl line 1 +72, near "m "(This is a test message at the end of the script)" hostn +ame" (Do you need to predeclare m?) Bareword found where operator expected at TDC-fabric-monitor.pl line 1 +72, near ") notify_action" (Missing operator before notify_action?) syntax error at TDC-fabric-monitor.pl line 172, near "/usr/bin" Execution of TDC-fabric-monitor.pl aborted due to compilation errors

Is there any way to format the system call so that Perl just passes that call to the outside shell? Should I be using something other than system() to call the postemsg command?

Thanks,

-Bill

Replies are listed 'Best First'.
Re: Launching an external command from Perl in Linux
by kennethk (Abbot) on May 05, 2009 at 15:55 UTC
    As the system documentation states, you should be passing a list of strings, not barewords as you've done.

    As well, I note that you are assigning the return value from system. From what you've named your variable, I don't think it returns what you think it does. From the documentation:

    The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below). See also "exec". This is not what you want to use to capture the output from a command, for that you should use merely backticks or qx//, as described in "`STRING`" in perlop. Return value of -1 indicates a failure to start the program or an error of the wait(2) system call (inspect $! for the reason).
Re: Launching an external command from Perl in Linux
by JavaFan (Canon) on May 05, 2009 at 15:53 UTC
    System takes a single string, or a list of strings as arguments.

    In Perl, strings are surrounded by quotes. Your code doesn't have them.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://761969]
Approved by salva
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2022-07-06 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?