LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-20-2017, 04:23 PM   #1
CrabbyAbby2
LQ Newbie
 
Registered: Aug 2017
Posts: 3

Rep: Reputation: Disabled
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
 
Old 08-20-2017, 06:06 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,549

Rep: Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085
It doesn't delete it after the "final_testing" test. It deletes it before it does that test.

Also, I notice a syntax error:
Code:
if [ "$1"== *'safe_rm' ] ;
needs a space before the "==".
 
1 members found this post helpful.
Old 08-20-2017, 10:20 PM   #3
CrabbyAbby2
LQ Newbie
 
Registered: Aug 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you for your feedback. I rearranged the order and my script stopped deleting. Thank you!
 
Old 08-21-2017, 03:17 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,746

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
Have $1 in quotes if it's in command arguments, to prevent it from expansion.
Code:
movefile_inode "$1"
Also [ ] is a command
Code:
if [ ! -e "$1" ]
The == operator and glob pattern is only defined for [[ ]] that is not a command.
Code:
if [[ $1 == *'safe_rm' ]]
A variable assignment is not a command
Code:
fileName=$1
inode=$(ls -i "$1" | cut -d" " -f1)
If in doubt, put in quotes!
$1_ is not clear, better have
Code:
echo "${1}_$inode:$path"
or
Code:
echo "$1"_"$inode:$path"
 
Old 08-21-2017, 06:55 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,518

Rep: Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555
do not use the keyword readlink as a variable
 
Old 08-21-2017, 09:51 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,549

Rep: Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085
Quote:
Originally Posted by pan64 View Post
do not use the keyword readlink as a variable
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.
 
1 members found this post helpful.
Old 08-21-2017, 11:09 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,518

Rep: Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555Reputation: 5555
Quote:
Originally Posted by rknichols View Post
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
 
Old 08-21-2017, 01:27 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,549

Rep: Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085
Quote:
Originally Posted by rknichols View Post
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 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script for deleting files james jackson Programming 8 08-01-2012 01:46 AM
A script for deleting user , if he has no login shell. meandsushil Linux - Newbie 4 11-26-2010 09:51 AM
[SOLVED] Silencing the line "echo test > test/test.txt" in a shell script Arenlor Linux - General 2 06-18-2010 01:37 PM
Shell script deleting oracle backup? Mirge Programming 2 03-10-2010 01:21 PM
my first shell script isn't working extendedping Programming 3 02-26-2007 02:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration