Why is my safe_rm shell script deleting after I test that safe_rm isn't being deleted?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why is my safe_rm shell script deleting after I test that safe_rm isn't being deleted?
Hello Everyone!
I am creating a bash script to ensure that the file that the user creates is being "deleted" (its moving the file to the recycle bin which is $HOME/deleted). In my attached code, steps 1-6 run the way that it is supposed to run. However, when I reach my last part to test that the file being "deleted" isn't safe_rm, it shows the error message that I created and I gave it the correct exit status but my whole script deletes. Luckily I have copies of it but I am wondering why its doing it. I've tried using readlinks but it appears to work for loops and my code isn't supposed to have any. I've attached a copy of my script. The issue that I am having is at #7. Am I supposed to add wild cards to my script to prevent deletion? Any suggestions and tips are greatly appreciated. Thank you very much everyone!
Code:
#!/bin/bash
##2.
create_directory(){
if [ ! -d $HOME/deleted ] ;
then
mkdir $HOME/deleted
fi
}
create_directory
## This will create a deleted directory in the home directory. If the the directory exist then the script will announce it.
##3.
fileName=$1
## This provides an argument for a filename.
##4.
error_checking(){
fileName=$1
if [ ! -e $1 ] ;
then
echo "safe_rm :cannot remove '$1' : No such file or directory"
exit 1 #will stop the script if there is no file or directory
elif [ $# -eq 0 ] ;
then
echo "safe_rm : cannot remove '$1' : Missing operand"
exit 1
elif [ -d $1 ] ;
then
echo "safe_rm : cannot remove '$1' : This is a directory"
exit 1
fi
}
error_checking $1
## This will test the three error conditions based on the file given.
##5 and ##6.
movefile_inode (){
fileName=$1
inode=$(ls -i $1 | cut -d" " -f1)
touch $HOME/.restore.info
readlink=$HOME/deleted
path=$(readlink -m $1)
echo "$1_$inode:$path">> $HOME/.restore.info ## This appends the file with its inode and absolute path into the hidden file .restore.info in the $HOME directory.
mv $1 $HOME/deleted/$1_$inode ##This will move the file into the recycle bin and the format will be filename_inode.
}
movefile_inode $1
##7.
final_testing(){
fileName=$1
if [ "$1"== *'safe_rm' ] ;
then
echo "Attempting to delete safe_rm - operation aborted"
exit 1
fi
}
final_testing $1
The name "readlink" is not a shell keyword, at least not in bash. It just happens to be one of a huge number of available external commands. Using it as a variable name isn't going to confuse the shell.
The name "readlink" is not a shell keyword, at least not in bash. It just happens to be one of a huge number of available external commands. Using it as a variable name isn't going to confuse the shell.
Yes, I was not precise enough. It won't confuse the shell, but the user....
sooner or later
The name "readlink" is not a shell keyword, at least not in bash. It just happens to be one of a huge number of available external commands. Using it as a variable name isn't going to confuse the shell.
Quote:
Originally Posted by pan64
Yes, I was not precise enough. It won't confuse the shell, but the user....
sooner or later
The same could be said for any of the other thousands of names in $PATH directories that someone happened to be familiar with as a command name.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.