Wednesday, July 17, 2013

write a Bourne shell script that takes a file name and a directory name as command line arguments and removes the file if it is found under the given directory and is a simple file. If the file(the first argument) is a directory, it is removed (including all the files and subdirectories under it).

#!/bin/bash
#########################################################
# File Name     :       sy_2_g
# Author:       :       Suresh Yadagiri
# Date          :       August 10,2010
# Descriptiom   :
#  write a Bourne shell script that takes a file name and
#  a directory name as command line arguments and removes
#  the file if it is found under the given directory and
#  is a simple file. If the file(the first argument) is a
#  directory, it is removed (including all the files and
#  subdirectories under it).
#
#########################################################
function printusage(){
echo -e "\nusage:\n$0 filename directoryname"
echo "    or   "
echo -e "$0 directoryname directoryname"
exit 1
}
if (( $# < 2 || $# > 2 ))
then
    echo "Invalid arguments"
    printusage
fi

if [ ! -d $2 ] ; then
     echo " $2 is not a directory or exists"
    printusage
fi

if [ ! -d "$2/$1" ] && [ ! -f "$2/$1" ] 
then
    echo "\"$1\" is not a directory or a file inside \"$2\"."
    printusage
fi

if [ -d "$2/$1" ] ; then
    echo "\"$1\" directory removed from $2"
    rm -r "$2/$1"
else
    echo "\"$1\" file removed from $2"
    rm -f "$2/$1"
fi

No comments:

Post a Comment