in reply to printing during process

It's hard to tell exactly what you mean when you refer to "shell command", particularly because you don't show us anything you've tried, but here's a simple Perl snippet that shows how to do what you want.

#!/usr/bin/perl use strict; use warnings; open my $fh, '<', 'file.txt' or die "Can't open file: $!"; while ( my $line = <$fh> ){ chomp $line; # do stuff print "$line\n"; }

Replies are listed 'Best First'.
Re^2: printing during process
by Anonymous Monk on Apr 05, 2012 at 07:07 UTC
    I am running a script to scan my website for vulnerabilities, when it finds them I want it to print the page, then continue, so it is running a while statement.

    I don't run it in a browser, I run it in shell:
    perl scanSite.cgi shell=1 dir=pathToStart
    So the script runs for 1 hundred thousand scans, always looking for vulnerabilities and sleeping 1 second in between, I have it print a period every 1 minute so that I know it is still running and when it finds something that matches what I have it looking for on the page, it prints the url and what it found.

    The problem is the print commands do not print anything, no periods or anything, but if I put a exit command in then it prints everything all at one time.

    What I want it to do is print stuff as it comes across the print command and not wait until the script is done with everything and comes across the exit command.

    Is there a perl command to have it print the output as it comes across it and not wait until the script is done?

    Thanks,
    Richard

      The only way anyone will know how to help you is if you post the relevant code from the CGI file you're using. However, the first thing that comes to mind is autoflush:

      local $| = 1;
        Here is an example:
        sub scanUrl { my $_url = $in{url}; my $_startingNumber = 0; my $_tnum = 0; my $_endingNumber = 100000; while($_endingNumber > $_startingNumber) { if($_tnum == 10) { print "."; $_tnum = 0; } sleep(1); } }
        That does not have any of my tracking code in it, but it is an example, if you run that, it never prints a period until it finishes. How do I get it to print the things I want it to, before it finishes the subroutines or while statement?

        thanks,
        Richard
      "the script runs for 1 hundred thousand scans, always looking for vulnerabilities and sleeping 1 second in between"

      So the sleep process, alone ((100_000/60)/60), eats up nearly 28 hours? Is it even sane to spread your output over more than a day?

        It would be if: the site(s) are not your own and don't want to set off a scanning alert in the ISP, don't intentionally want to dos the target and don't know that sleep can sleep for less than a second at a time, or running the program at a slow pace to sample its output until the output is what is desired.

        Looks almost like a piece of a brute force dos widget with a one second delay :)