0

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.

3
  • also chusers and chdirs is a flat file containing usernames and directories to check. Aug 6, 2017 at 18:45
  • The 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. Aug 6, 2017 at 19:20
  • 1
    I see some really horrible stuff in Linux magazines tbh. Better you reformulate your question by writing a description of specifically what you want to do, and what help you need with that, otherwise we are all going to pick holes in the script in the comments, without really knowing what the desired output is, instead of giving you a useful answer (although Florian Diesch's answer should solve the painfully obvious issue causing the command not found errors...)
    – Zanna
    Aug 6, 2017 at 19:25

1 Answer 1

1
PATH=/bin:/user/bin

should be

PATH=/bin:/usr/bin

Both awk and du are in /usr/bin:

$ type -p du;type -p awk
/usr/bin/du
/usr/bin/awk

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .