Results 1 to 1 of 1

Thread: Counter for loops or looping through files

  1. #1
    Join Date
    Mar 2007
    Location
    Your Closet
    Beans
    380
    Distro
    Ubuntu 10.04 Lucid Lynx

    Thumbs down Counter for loops or looping through files

    Looks like this.
    Code:
    for ((i = 0; i < 101; i++)); do echo 100 $i; sleep 0.05; done | counter.pl
      18% [=========                                             ]  18/100 
      41% [======================                                ]  41/100 
      69% [=====================================                 ]  69/100 
     100% [======================================================] 100/100
    Very simple to use just send the program standard output that looks like this.
    Code:
    100 0
    100 10
    100 20
    100 100
    Example 1.
    Code:
    for ((i = 0; i < 101; i++)); do echo 100 $i; sleep 0.05; done | perl counter.pl
    Example 2.
    Code:
    counter=1
    files=(*)
    for file in "${files[@]}"; do
    echo ${#files[@]} $((counter++))
    # replace sleep command with something that uses $file
    sleep 0.25
    done | perl counter.pl
    Script.
    Code:
    #!/usr/bin/env perl
    
    $|++;
    
    while (readline) {
    	next unless /(\d+)\s+(\d+)/;
    	exit if $1 eq 0;
    
    	my $cols = qx/tput cols/;
    	chomp $cols;
    
    	my $s3s = 1 + (length $1) * 2;
    	my $s2s = $cols - $s3s - 10;
    
    	my $s1 = ($2 != 0 ? int 100 / ($1 / $2) : 0);
    	my $s2 = ($2 != 0 ? $s2s / ($1 / $2) : 0);
    
    	printf "\r % 3s%% [%-${s2s}s] % ${s3s}s ", $s1, "=" x ($s2 > $s2s ? $s2s : $s2), "$2/$1";
    }
    
    print "\n";
    Last edited by bashologist; April 9th, 2011 at 05:38 PM.
    ...

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •