Good evening everyone!
I was able to fix my safe_rm code to "delete" my files. Although my code to restore the files looks great and works for the most part, I am having issues with some part of my code, particularly trying to overwrite the file and to move the file to the original directory based on its path. I have attached the code below. Any and all suggestions are greatly appreciated. Thank you!
Code:
#!/bin/bash
##2.
fileName=$1
inode=$2
## This will be the name of the restored files that is in the recycle bin.
##3.
movingfile(){
if [ -e $HOME/deleted/$1_$2 ]
then
mv $HOME/deleted/$1_$2 $(grep $1_$2 $HOME/.restore.info | cut -d":" -f2)
fi
}
## This will retrieve the files in the home deleted bin and retrieve the files that are in the hidden file.
##4.
safe(){
fileName=$HOME/deleted/$1_$2
if [ $# -eq 0 ]; then
echo "safe_rm: missing operand"
exit 1
fi
if [ ! -e $fileName ] ;
then
echo "safe_rm_restore :cannot restore '$1': No such file or directory"
exit 1
fi
}
## This tests the conditions of the retreived file on whether it exists or is missing an operand.
##5.
restoreFile(){
fileName=$HOME/deleted/$1_$2
if [ -e $(grep $1_$2 $HOME/.restore.info | cut -d":" -f2) ] ;
then
read -p "File currently exists. Do you want to overwrite y/n?:" word
if [[ $word = "yes" || $word = "Y" || $word = "y" ]] ;
then
mv $HOME/deleted/$1_$2 $(grep $1_$2 $HOME/.restore.info | cut -d":" -f2) && exit 0 ## I used an if-then statement in order for the user to select what they want to do to the restored file.
fi
if [[ $word = "n" ]] ;
then
exit 1
else
echo "Invalid Option"
exit 1
fi
fi
}
##6.
restored(){
remove=$(grep -v $1_$2 $HOME/.restore.info | tee $HOME/.restore.info)
if [[ ! -e $HOME/deleted && -e $(grep $1_$2 $HOME/.restore.info | cut -d":" -f2) ]];
then
echo $remove
fi
}
safe $1 $2
movingfile $1 $2
restoreFile $1 $2
restored $1 $2