Re: Variable value in while loop
From: Ed Morton (morton_at_lsupcaemnt.com)
Date: 06/29/05
- Next message: DozyRosy: "Re: Error: *: Parameter not set"
- Previous message: Ed Morton: "Re: while read : slow"
- In reply to: alex221_at_pisem.net: "Variable value in while loop"
- Next in thread: Bill Marcum: "Re: Variable value in while loop"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: DozyRosy: "Re: Error: *: Parameter not set"
- Previous message: Ed Morton: "Re: while read : slow"
- In reply to: alex221_at_pisem.net: "Variable value in while loop"
- Next in thread: Bill Marcum: "Re: Variable value in while loop"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|