Search This Blog

Tuesday, September 10, 2013

A simple script that will help you to understand the usage of [if elif else fi] and check whether file or directory.

vi delete.sh

#code goes here.


#------------------------------------------------------------------------------------------

#!/bin/bash

echo -n "Enter the filename : ";
read myfile;
echo -n "Are you sure (Yes/No)?"
read confirm;

#Checking the entered file type

if [ -f $myfile ]; then
value="File"
elif [ -d $myfile ]; then
value="Directory"
else
value="Special file"
fi

#user confirmation for deletion

confirm="$(echo ${confirm} | tr 'A-Z' 'a-z')"
if [ "$confirm" == "yes" ]; then

#deleting the file/directory

[ -f $myfile ] || [ -d $myfile ] &&  /bin/rm -r $myfile && echo "$value $myfile has been deleted"|| echo "Error - Unable to locate the file $myfile"

else
echo " Program exiting...."
fi
#------------------------------------------------------------------------------------------

No comments: