du from list with du of list total

Hi there

i am relatively new to shell scripting and am stuck. I wrote (and borrowed parts of) a script that will read input from a text file and echo the file name and then give me the du of that file right underneath it in a nice clean way only after telling me the number files on which it will perform these actions. I would like it to also give me the combined du of all of the files in the list. Does anybody know how to do this?

here is my script:

#!/bin/bash
if [ ! $1 ]; then
echo missing arguments\(s\)
echo du-from-list.sh \<works-list\>
exit 1
fi

WORKLIST=$1
WORKSIZE=$du -hs /Volumes/image/$line

echo Total Number of Works
cat /Users/tbrc/scripts/ctc/lists/O1.txt | wc -l
sleep 5

exec 3<&0
exec 0<$WORKLIST
while read line
do

echo $line
du -hs /Volumes/image/$line | awk -F/ '\{print $1\}'

done
exec 0<&3

exit 0

Ideally i could something like
echo "total du"
and then the command

any help helps.

Thanks,
Movomito

There's no need for the fancy execs, just do this:

while read line
do
    # some stuff
done < $WORKLIST

To get a total, just store the output of the du command in a variable, and add them up as you go, something like this:

    echo $line
    usage=`du -hs /Volumes/image/$line | awk -F/ '{print $1}'`
    total_usage=`expr $total_usage + $usage`
    echo $usage

First off I am home taught so it may be that there is something that i am missing here. If i don't first run an exec how can i "read line" from workslist? But i did see what it was attempting and kept my execs and wrote the body of m script like this:

exec 3<&0
exec 0<$WORKLIST
while read line
do

echo $line
usage=\`du -hs /Volumes/image/$line | awk -F/ '\{print $1\}'\`
total_usage=\`expr $total_usage \+ $usage\`
echo $usage

\#echo $line
\#du -hs /Volumes/image/$line | awk -F/ '\{print $1\}'

done
exec 0<&3

and i got this error:

W00EGS1017426(this is the dir name)
expr: syntax error
3.2G(this is the individual directories size)

as you can see i am still getting the individual size output.

Thanks for the help, maybe you can make more sense of this than I can.

Have a read of the REDIRECTION section of the bash man page to understand how input and output redirection works. The exec commands you were using were for duplication of file descriptors, which is useful in some scenarios, but unnecessarily complicated for your requirements.

For the expr calculation problem, it's because expr doesn't understand units in G. Take the -h option out of the du command line so that it doesn't output units of MB and GB.