I was going through a tutorial on the latest Linux Shell Handbook for Linux Pro mag and I am having trouble getting this block of code to work. When I run the script it tells me that du
and awk
commands are not found.
#!/bin/bash
PATH=/bin:/user/bin
. /usr/local/sbin/functions.bash
printf "USER\tGB USED\n"
for WHO in $(</user/local/sbin/chusers); do
HOMESUM=`eval du -s -$WHO |awk '{print $1}'`
TMPLIST=$( ls -lR --block-size 1024 $(</user/local/bin/chdirs) | egrep "^.......... +[0-9]+ $WHO" | awk '{print $5}' )
TSUM=0
for N in $TMPLIST; do
TSUM=$(( $TSUM+$N ))
done
TOT=$(( $HOMESUM+$TSUM ))
to_gb $WHO $TOT
done
Here is the to_gb
function from function.bash
to_gb()
{
local MB D1 D2 USER
USER=$1
MB=$(( $2/1024))
D1=$(( $MB/1000))
D2=$(( $MB-($D1*1000) ))
printf "%s\t%s\n" $USER $D1.${D2:0:1}
return
}
I have been able to run each command du -s ~username
and awk '{ print $1 }'
from the command line with no problem and seen the expected output but the script fails to work.
to_gb()
function seems rather limited. I like to use this function that converts the file size to Bytes, KiB, MiB, GiB, TiB, EiB, PiB, YiB and ZiB.