Hi Everyone,
I am trying to create a recycle bin in unix where the user can o into the folder and have the opportunity to restore the file to its original folder.
I have created two vi: remove and restore23.
remove is working perfectly fine however the restore23 fail to execute its purpose.
below are the vi script for both:
remove
Code:
#!/bin/bash
#Global Variables
recyclingBin=~/deleted
restore=~/deleted/.restore.info
function validate()
{
if [ $# -eq 0 ]
then
echo "**Please provide the name of the file to restore**"
elif [ ! -e $recyclingBin/$1 ]
then
echo "**restore: File \`$1' doesn't exist**"
fi
}
function restore()
{
abspath=$(grep $1 $restore | cut -d":" -f2)
dir=$(dirname $abspath)
if [[ -d $dir ]]
then
mv $recyclingBin/$1 $abspath
grep -v $1 $restore > temp && mv temp $restore #removes the restored file record from .restore.info
else
mkdir -p $dir
mv $recyclingBin/$1 $abspath
grep -v $1 $restore > temp && mv temp $restore
fi
}
function checkExist()
{
abspath=$(grep $1 $restore | cut -d":" -f2)
if [ ! -e $abspath ]
then restore $1
else read -p "Do you want to overwrite? y/n: " option
case $option in
[Yy]*) restore $1 ;;
*) exit 0;;
esac
fi
}
validate $1
checkExist $1
******************************
restore23
Code:
#!/bin/bash
#Global Variables
recyclingBin=~/deleted
restore=~/deleted/.restore.info
#checking if the file exist
function validate()
{
if [ $# -eq 0 ]
then
echo "**Please provide the name of the file to restore**"
exit 1
elif [ ! -e $recyclingBin/$1 ]
then
echo "**restore: File \`$1' doesn't exist**"
exit 1
fi
}
#restore
function restore()
{
abspath=$(grep $1 $restore | cut -d":" -f2)
dir=$(dirname $abspath)
if [[ -d $dir ]]
then
mv $recyclingBin/$1 $abspath
grep -v $1 $restore > temp && mv temp $restore #removes the restored file record from .restore.info
else
mkdir -p $dir
mv $recyclingBin/$1 $abspath
grep -v $1 $restore > temp mv temp $restore
fi
}
#confirmation required
function checkExist()
{
abspath=$(grep $1 $restore | cut -d":" -f2)
if [ ! -e $abspath ]
then restore $1
else read -p "Do you want to overwrite? y/n: " option
case $option in
[Yy]*) restore $1 ;;
*) exit 0;;
esac
fi
}
sed -i "/$1/d"~/deleted/.restore.info
validate $1
checkExist $1
*********************
everytime i run the sh restore23 command along a file in the deleted folder i get the error below:
Code:
$sh resorecopy try8_2802904
/bin/bash: resorecopy: No such file or directory
Can anyone help me figure out what am i doing wrong?
Many thanks