-1

I have a requirement that create a script name say ./123.sh when given a path to directory it should print the file or files with minimum, maximum and average file size . eg

$> ./123.sh /usr/share
Minimum file size: 1024
    /usr/share/lala.txt
    /usr/share/koko.txt
Maximum file size: 100234
    /usr/share/somewhere/else/test.tar.gz
Average file size: 5034

My code :

dir="$1"

if [ $# -ne 1 ]
then
    echo "please pass arguments" 
exit 0
fi


if [ -e $dir ]
then
printf "minimum file size: %s\n\t%s\n" \
 $(du $dir -hab | sort -n -r | tail )

printf "maximum file size: %s\n\t%s\n" \
 $(du $dir -ab | sort -n | tail -1)

printf "average file size: %s"
du $dir -sk | awk '{s+=$1}END{print s/NR}'
else
echo " directory doesn't exits"
fi

if [ -d "ls -1A -- $dir" ]
then
echo " directory is empty "
fi

If I execute my script with ./123.sh the output should be:

"please pass arguments".

similar to script with arguments ./123.sh hdkjflkjds

"please pass correct arguments".

Similar to script with arguemnts ./123.sh /usr/share The output should display.

$> ./123.sh /usr/share
    Minimum file size: 1024
        /usr/share/lala.txt
        /usr/share/koko.txt
    Maximum file size: 100234
        /usr/share/somewhere/else/test.tar.gz
    Average file size: 5034

Finally If I execute the script with arguments(i.e empty directory path) say /usr/bin $./123.sh /usr/bin

It should display:

"Directory is empty".

Rather its showing for me an error like this.

$> ./123.sh /usr/bin
        Minimum file size: 1024
            /usr/bin/lala.txt
            /usr/bin/koko.txt
        Maximum file size: 100234
            /usr/bin/somewhere/else/test.tar.gz
        Average file size: 5034

can anyone check my code fidn out the mistake what I have done.

1 Answers1

1

According to man test (http://unixhelp.ed.ac.uk/CGI/man-cgi?test) you'd better use [ -d $dir ] where you currently have [ -e $dir ].

And where you have [ -d "ls -1A -- $dir" ] I suppose you want to test if the result is empty which you can test better with [ -z "$(ls -1A -- $dir)" ]

A sample nested test can be:

if [ -d $dir ]
then
    if [ -z "$(ls -1A -- $dir)" ]
    then
         echo "Directory exists but is empty"
    else
         ... do your magic ...
    fi
else
    echo "directory does not exist"
fi
Lambert
  • 12,680
  • I did use the command which you mentioned [ -z "$(ls -1A -- $dir)" ] but its showing error as: minimum file size: 4096 /home/sreekanth/testdir/aki/schatz maximum file size: 4096 /home/sreekanth/testdir/aki/schatz average file size: 4 directory is empty . Its displaying with all sizes. All I need is only "directory is empty" – buddha sreekanth May 11 '15 at 11:55
  • That is correct since the directory exists and contain two special directories called . and ... You should nest your tests. – Lambert May 11 '15 at 12:00
  • what is nest? so what should I have to do inorder to get only output as "directory is empty" – buddha sreekanth May 11 '15 at 12:14
  • See my edited answer. With '... do your magic ...' I refer to your min/max/avg commands. – Lambert May 11 '15 at 12:31
  • How to find a same file size in any directory to my code i.e $(du $dir -hab | sort -n -r | tail )-when I used this command its showing with all least file sizes in a directory. $(du $dir -hab | sort -n -r | tail -1) - this shows the least file size in a directory,but I need a least same file size in a directory – buddha sreekanth May 11 '15 at 14:43