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.
.
and..
. You should nest your tests. – Lambert May 11 '15 at 12:00