Wednesday, July 17, 2013

write a shell script that takes a directory as a required argument and displays the name of all zero length files in it. Do appropriaet error checking

#!/bin/bash
##########################################################################
# File Name:     sy_2
# Author:    Suresh Yadagiri
# Written:    July 25,2010
# Purpose:    To display zero size files in directory
# Problem:
#     write a shell script that takes a directory as a required argument
#    and displays the name of all zero length files in it.
#    Do appropriate error checking
#
#      Usage: ./sy_2
#
#
##########################################################################

if [ $# = 0 ]
 then
    echo "One Argument expected"   
    echo "Usage: $0 directory_name"
    exit 1
elif [ $# != 1 ]
then
    echo "only one argument expected"
    echo "Usage : $0 directory name"
       exit 1
elif [ ! -d "$1" ]
then
    echo "$1 is not a directory or not exists"
    echo "Usage: $0 directory name"  
    exit 1
fi
directory=$1
#Get file count in the given directory ; if directory
#is emptry then display appropriate message and
#quit
file_count=`ls $directory | wc -w`  #it get no.of files in directory
if [ $file_count -eq 0 ]
then
    echo "$1 is Empty directory"
    exit 0
fi
#for each file in the direcotry specified, find the
# file size. and display if its empty file
echo "zero length files in $directory are "
ls "$directory" | more |
while read file
do
filepath="$directory"/"$file"
#echo $file
if [ -f "$filepath" ]
then
    set -- `ls -l "$filepath"`
#    echo "file size is : $5 "
    file_size=$5
    if [ $file_size -eq 0 ]
    then
        #echo "$9"
        echo "$file"
    fi
fi
done




No comments:

Post a Comment