Variable value in while loop

alex221_at_pisem.net
Date: 06/29/05


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



Relevant Pages

  • Re: Sum of du -sk
    ... I am trying to print the sum of all folders pertaining to a specific ... echo $TOTAL ... Now when i execute the script i get an error: ...
    (comp.unix.shell)
  • Sum of du -sk
    ... I am trying to print the sum of all folders pertaining to a specific ... echo $TOTAL ... the PATH variable output is somthing like this: ... Now when i execute the script i get an error: ...
    (comp.unix.shell)
  • Re: Variable value in while loop
    ... > Please consider the following simple script ... > declare -i size ... > while read size fname; ... read total_deleted sum < /tmp/totsum ...
    (comp.unix.shell)
  • simple adding numbers logic help needed
    ... Script that will give me the total of all even numbers. ... echo "You entered $n Sum of all odd digit is $sum" ... You entered 5 Sum of all odd digit is 0 ...
    (comp.unix.shell)
  • Re: Sum of du -sk
    ... I am trying to print the sum of all folders pertaining to a specific ... user with following script: ... exported variables. ... echo $TOTAL ...
    (comp.unix.shell)