Variable value in while loop
alex221_at_pisem.net
Date: 06/29/05
- Next message: Ed Morton: "Re: while read : slow"
- Previous message: Ed Morton: "Re: AWK or otherway to convert transpose Columns to Rows"
- Next in thread: Ed Morton: "Re: Variable value in while loop"
- Reply: Ed Morton: "Re: Variable value in while loop"
- Reply: Bill Marcum: "Re: Variable value in while loop"
- Reply: Bill Marcum: "Re: Variable value in while loop"
- Reply: Bill Seivert: "Re: Variable value in while loop"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 29 Jun 2005 06:38:27 -0700
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.
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
although the script does OK, except the problem i stated first.
Thanks in advance.
Alex
- Next message: Ed Morton: "Re: while read : slow"
- Previous message: Ed Morton: "Re: AWK or otherway to convert transpose Columns to Rows"
- Next in thread: Ed Morton: "Re: Variable value in while loop"
- Reply: Ed Morton: "Re: Variable value in while loop"
- Reply: Bill Marcum: "Re: Variable value in while loop"
- Reply: Bill Marcum: "Re: Variable value in while loop"
- Reply: Bill Seivert: "Re: Variable value in while loop"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|