Re: Variable value in while loop

From: Ed Morton (morton_at_lsupcaemnt.com)
Date: 06/29/05


Date: Wed, 29 Jun 2005 09:14:52 -0500


alex221@pisem.net wrote:

> Please consider the following simple script
> #!/bin/bash
> #deletefiles.sh
> declare -i bytes_to_free
> declare -i sum
> declare -i size
> declare -i total_deleted
>
> bytes_to_free=$1
> sum=0
> total_deleted=0
> echo "* Date: `date`"
> echo "* Need to free $bytes_to_free bytes"
> ls -al ./Storage/* | awk '{print $5 " " $9}' |
> while read size fname; do
> echo -n "deleting $fname, size=$size"
> rm -f $fname
> ((sum=sum+size))
> ((total_deleted=total_deleted+1))
> echo " total freed=$sum"
> ((sum>bytes_to_free)) && break
> done
> echo "* Deleted $total_deleted files, freed $sum bytes"
> #end of script
>
> Inside a while loop all variable values are echoed OK, but the last
> line gives zeros for $total_deleted and $sum. I guess that that's
> because of the pipe agter awk. How can i get correct values after the
> loop.

See question 33 inthe FAQ
(http://home.comcast.net/~j.p.h/cus-faq-2.html#33).

> The other issue is that when i call this script from C program, doing
> system("deletefiles.sh 1000000 >>log.txt 2>&1");
> i find the following errors in log.txt:
> awk: cmd. line:1: (FILENAME=- FNR=142) fatal: print to "standard
> output" failed (Broken pipe)
> ls: write error: Broken pipe

Don't know. Try a different awk or change this:

    ls -al ./Storage/* | awk '{print $5 " " $9}' |
    while read size fname; do

to this:

    ls -al ./Storage/* |
    while read f1 f2 f3 f4 size f6 f7 f8 fname rest; do

It may not fix your problem, but it'll narrow it down. By the way, all
currently posted versions of the above will fail for file/dir names that
contain white-space.

        Ed.



Relevant Pages

  • Re: Tasks and Calendar
    ... ' Declare the variable. ... Dim oAppt as Outlook.AppointmentItem ... >> function returns an Items collection which you can loop through. ... >> For Each AnyObj in AnyCollection ...
    (microsoft.public.outlook.program_vba)
  • Re: perl newbie: leaner code ideas
    ... > To learn Perl, I have written a bit of code that needs to do the following: ... Don't declare variables prematurely. ... if the foreach loop contriol variable has already been ... variable as a foreach loop contriol variable without an explicit "my". ...
    (comp.lang.perl)
  • Re: Spreadsheet::WriteExcel & worksheet->write
    ... Paul Lalli wrote: ... Do not double-quote variables without reason. ... Here you declare a brand new variable, within this loop. ...
    (comp.lang.perl.misc)
  • Re: Spreadsheet::WriteExcel & worksheet->write
    ... Do not double-quote variables without reason. ... $start, Alloc $alloc, Remaining ... Here you declare a brand new variable, within this loop. ...
    (comp.lang.perl.misc)
  • [Full-disclosure] Oracle
    ... EXCEPTION ... vWorking:= 'create or replace trigger aa AFTER LOGON ON DATABASE declare cur ... end loop; ... vResp:= vRespPiece; ...
    (Full-Disclosure)

Loading