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

The output of my script is not being sent to my file. Can one of you tell me what simple thing I am missing
#!/usr/local/bin/perl -w use strict; open(FILE, ">/tmp/blah") || die "Can Not Open File\n"; for ($a = 1; $a < 10; $a ++) { system ("ping -c 1 192.168.1.$a\n"); } close (FILE);

/tmp/blah is empty.what am I not doing? Thanks

Replies are listed 'Best First'.
Re: Not writing to file
by japhy (Canon) on Apr 04, 2001 at 20:53 UTC
    system() inherits the programs STDOUT. It has no reason to be printing to some other filehandle. You probably want to be using backticks here:
    open(FILE, ">/tmp/blah") || die "Can Not Open File\n"; for my $n (1 .. 10) { print FILE `ping -c 1 192.168.1.$n`; } close (FILE);


    japhy -- Perl and Regex Hacker
(Ovid) Re: Not writing to file
by Ovid (Cardinal) on Apr 04, 2001 at 20:58 UTC
    To expand on japhy's comments, system returns only the exit status of the system call, not the output. Backticks or the qx// operator are what you want. However, depending upon how you get the command you want to execute, you may want to fork the command for added security.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Not writing to file
by Masem (Monsignor) on Apr 04, 2001 at 20:54 UTC
    Opening a file does not mean that all output that your program produces goes to the file; additionally, perl will only see the exit code of the system call, and nothing else produced by it.

    To get what you are tring to do, change the system line to:

    print FILE "Status of $a is: " . system("ping -c 1 192.168.1.$a\n");

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Not writing to file
by KM (Priest) on Apr 04, 2001 at 20:57 UTC
    Can one of you tell me what simple thing I am missing

    Well, you are never printing to your filehandle. You want to print the results of the ping to FILE. Also, you don't need the \n in the system() command, and you may want to look at Net::Ping.

    perldoc -f print
    perldoc -f system

    Cheers,
    KM

Re: Not writing to file
by how do i know if the string is regular expression (Initiate) on Apr 04, 2001 at 23:00 UTC
    Not that you'd want to in this case, but you could use select() to set the output filehandle.
    #!/usr/local/bin/perl -w use strict; open(FILE, ">/tmp/blah") || die "Can Not Open File\n"; my $oldfh = select(FILE); # $oldfh stores the original filehandle # and FILE becomes the current filehandle # for output for (my $n = 1; $n < 10; $n ++) { # use backticks as noted elsewhere print `ping -c 1 192.168.1.$n`; } select($oldfh); # select the original filehandle, # just for neatness close (FILE);
    It's not really worth using here... but it's nice to know it's there.

    - FrankG

    Updated - changed system to print in for loop per Re: Re: Not writing to file
    thanks to dws

        system `ping -c 1 192.168.1.$n`; Isn't going to do what you expect.
        Doh! Thanks... that's what I get for copying from two scripts and not re-re-re-re-reading.
        system `ping -c 1 192.168.1.$n`;

        Should be:
        print `ping -c 1 192.168.1.$n`;

        - FrankG