in reply to script outputs nothing/nada

That code does not even compile for me. Does it compile for you?
my $workdir=~/txt;
I changed that to:
my $workdir="~/txt";
but it would be better as:
my $workdir = "$ENV{HOME}/txt";
I also changed BAD-IPS to BADIPS everywhere. Those changes got the code to compile.

One explanation for not getting any output is that your while loop is never entered -- check to see if the bad-ips file is empty. Also, try to print something before the while loop.

Replies are listed 'Best First'.
Re^2: script outputs nothing/nada
by bluethundr (Pilgrim) on Jun 06, 2010 at 19:23 UTC
    Hi and thanks for playing! :-)

    Ok so it would seem (from the output..or lackthereof) of the debugging statements that the loop isn't even being entered. But _why_?

    here's the current state of affairs:

    #!/usr/bin/perl # use strict; # # my $workdir = "$ENV{HOME}/txt"; open (BADIPS), "< $workdir/bad_ips" or die "Couldn't open file: $!"; print "Can you hear me now?\n"; while (my $line = <BADIPS>) { chomp $line; print "add ns simpleacl bp-search-spammer DENY -srcIP $line -TTL + 43200"; print "Can you hear me now?\n"; } close BADIPS;
    This is what the output looks like:

    [bluethundr@lcent5-1:~/perl] $:./bad_ips.pl Can you hear me now?


    And I hear what you're saying about the input file (bad_ips) potentially being empty. But I gotta tell ya, it sure doesn't look that way to me! I am able to open the file and here is the output of ls:

    [bluethundr@lcent5-1:~/txt] $:ls -l bad_ips -rw-r--r-- 1 bluethundr bluethundr 822 Jun 5 04:24 bad_ips
    any thoughts? tx again!
      open (BADIPS), "< $workdir/bad_ips" or die "Couldn't open file: $!";

      If that's the code you're really using (it's different to your OP), try adding use warnings; after use strict;. That usage of open does something you really don't want (hint: it isn't trying to open bad_ips).

      Try:

      open BADIPS, "<", "$workdir/bad_ips" or die "Couldn't open file: $!";

      instead.

      You should also check the contents of bad_ips. It could be full of newlines.

        Hello, I have tried stripping the newlines out of the bad_ips text file with the following command.

        grep -ri s/"\n"// ~/txt/bad_ips


        And made the following changes to the code.
        #!/usr/bin/perl # use strict; use warnings; # # my $workdir = "$ENV{HOME}/txt"; open BADIPS, "<", "$workdir/bad_ips" or die "Couldn't open file: $!"; my $i = 1; while (my $line = <BADIPS>) { chomp $line; print "add ns simpleacl bp-search-spammer$i DENY -srcIP $line -T +TL 43200\n"; $i++; } close BADIPS;<br><br>
        That worked BEAUTIFULLY! Thank you!!!