Wednesday, July 17, 2013

Write a shell script that takes an ardinary file as argument and removes the file if its size is zero.otherwise, the script displays file's name, size, number of hard links,owner,and modify date (in this order) on one line.

#!/bin/bash
#############################################################################
# File Name:   sy_1                                                         #
# Author:       Suresh Yadagiri                                             #
# Written:      July 25,2010                                                #
# Purpose:                                                                  #
#      Write a shell script that takes an ardinary file as argument         #
#      and removes the file if its size is zero.otherwise, the script       #
#      displays file's name, size, number of hard links,owner,and modify    #
#      date (in this order) on one line.                                    #
#      you script must do appropriate error checking                        #
#                                                                           #
#     Usage: ./sy_1                                   #
#                                                                           #
#                                                                           #
#############################################################################

if [ $# = 0 ]
 then
        echo "One Argument expected"
        echo "Usage: $0 ordinary_file"
        exit 1
elif [ $# != 1 ]
then
        echo "only one argument expected"
        echo "Usage : $0 ordinary_file"
       exit 1
fi

filename=$1

if [ -d $filename ]
then
echo "File is directory"
echo "Usage: $0 ordinary_file"
exit 1
fi

if [ ! -f $filename ]
then
echo "File is not exists "
echo "Usage: $0 ordinary_file"
exit 1
fi

set -- `ls -al $filename`


if [ $5 -eq 0 ]
then
echo "Its empty file. file deleted"
rm -f $filename
exit 0
fi
echo "File is not empty. File detail are: "

#file name
#size
#no.of hard links
#owner
#modify date
echo -e "File Name:                     $9"
echo -e "File size:              $5 bytes"
echo -e "Hard Links:             $2"
echo -e "file Owner:             $3"
echo -e "File modification time :     $6 $7 $8"

No comments:

Post a Comment