Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

How to print the contents of system function to a .txt file

by astronogun (Sexton)
on Mar 12, 2013 at 03:55 UTC ( #1022914=perlquestion: print w/replies, xml ) Need Help??
astronogun has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks I would like to need a hand here regarding this matter hope you could help me on this.

I have this code saved in "" which is must print the system function to .txt file.

#!/usr/bin/perl -w $input = 'input.txt'; @output = system ("java -jar cmdline-jmxclient-0.10.3.jar - localhost: +1100 com.ogs.red5.client.manager:name=Manager getConnectedAgents>>inp +ut.txt");

When I checked the input.txt file there's no output or it didn't print the result there..

The result should be like this: (when I just use system function and not print it in .txt file) and I want this result to be in .txt file.

 03/12/2013 11:43:40 +0800 org.archive.jmx.Client getConnectedAgents: [6569]

Replies are listed 'Best First'.
Re: How to print the contents of system function to a .txt file
by kcott (Chancellor) on Mar 12, 2013 at 05:26 UTC

    G'day astronogun,

    While there's quite a few issues with the code you posted, I see nothing that would prevent appending to a text file:

    $ ls -l input.txt ls: input.txt: No such file or directory $ perl -e '$input = q{input.txt}; @output = system ("ls -l >> input.tx +t");' $ ls -l input.txt -rw-r--r-- 1 ken staff 16393 12 Mar 15:34 input.txt

    Here's some of the issues I found with your code:

    • Unless you have a very good reason not to, always put "use strict;" and "use warnings;" at the start of your code. See strict and warnings.
    • Use meaningful names. You've called your output file input.txt and assigned that to a variable called $input. You also don't use the variable $input: I'm wondering if that was just an oversight or is input.txt actually an input file!
    • system does not return an array but you're assigning its return value to @output. Again, I'm left wondering if this was just a poor naming choice coupled with a failure to check the documentation or are you actually expecting output from your java ... command to be placed in @output.
    • You really need check return values. system gives a good description of how to go about this; it also mentions use of the autodie pragma.

    I'd suggest something like the following code as a starting point from which you can inspect return values and investigate your problem further.

    $ perl -e ' use strict; use warnings; use autodie qw{:all}; my $output_file = q{output.txt}; my $return_value = system "ls -l >> $output_file"; '

    You'll note both here, and earlier, that I've used a simple ls -l command to test the code. I'd suggest you do the same, to check whatever file-appending problem you're experiencing, before trying with "java -jar cmdline-jmxclient-0.10.3.jar - localhost:1100 com.ogs.red5.client.manager:name=Manager getConnectedAgents" which may have its own issues that could be masking your reported problem.

    -- Ken

Re: How to print the contents of system function to a .txt file
by vinoth.ree (Monsignor) on Mar 12, 2013 at 04:37 UTC

    Simple one

    use strict; use warnings; use autodie; open (my $file, '>', 'result.txt'); print $file `java -jar cmdline-jmxclient-0.10.3.jar - localhost:1100 c +om.ogs.red5.client.manager:name=Manager getConnectedAgents`;
Re: How to print the contents of system function to a .txt file
by 2teez (Vicar) on Mar 12, 2013 at 05:38 UTC

    @output = system ("....")
    to collect the output of the command you want to use the backticks @output = `...` or @output = qx{...} not a system function.
    Please check Quote-Like-Operators.
    However, to know if your command "java -jar cmdline-jmxclient-0.10.3.jar - localhost: +1100 com.ogs.red5.client.manager:name=Manager getConnectedAgents" works is a different story altogehter.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: How to print the contents of system function to a .txt file
by Anonymous Monk on Mar 12, 2013 at 03:58 UTC

      I'm using debian and the .txt file is in the directory of "/home/netadmin/Desktop" along with the .pl file when i run it using the "./" the generated .txt file has no output there.

        when i run it using the "./" the generated .txt file has no output there.

        Not what I asked, the command you're trying to execute using system, what happens when you type it into the shell?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022914]
Approved by vinoth.ree
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2018-02-18 05:34 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (250 votes). Check out past polls.