RecycleBin Script Restore


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting RecycleBin Script Restore
# 1  
Old 10-21-2013
RecycleBin Script Restore

Hi there I am making a recycle bin script in linux that consists of three scripts delete, trash and restore.

The problem is with my restore script, it is using a Paths text file to store file locations when it is deleted, so that if the user restores a file out of the trash bin (recyclebin) the restore script will put it in the directory it originally was.. Below is my restore script could anyone possibly tell me a way how to edit the script so if a file is restored it will not overwrite a file if there is a naming conflict. Otherwise the restore script works successfully.

Code:
      #!/bin/sh
      #!/bin/bash
      if [ $1 ]; then
          echo "Restoring File..."
          grep $1 binfiles/binscripts/Paths
          restore=`grep $1 binfiles/binscripts/Paths'
          mv ~/RecycleBin/$1 $restore
          sed -i '$ d' ~/binfiles/binscripts/Paths
      else 
         echo "That files doesn't exist.";
      fi

$1 represents the file, what I am trying to work out if $1 is moved back to its original directory and a file with the same name is already there how can i prompt the user to ask if they wish to overwrite it or not? and Paths is the text file the original directories are stored in. Thanks in advance

Last edited by Don Cragun; 10-21-2013 at 08:45 PM.. Reason: Add CODE tags
# 2  
Old 10-21-2013
Quote:
Originally Posted by zinn
Hi there I am making a recycle bin script in linux that consists of three scripts delete, trash and restore.

The problem is with my restore script, it is using a Paths text file to store file locations when it is deleted, so that if the user restores a file out of the trash bin (recyclebin) the restore script will put it in the directory it originally was.. Below is my restore script could anyone possibly tell me a way how to edit the script so if a file is restored it will not overwrite a file if there is a naming conflict. Otherwise the restore script works successfully.

Code:
      #!/bin/sh
      #!/bin/bash
      if [ $1 ]; then
          echo "Restoring File..."
          grep $1 binfiles/binscripts/Paths
          restore=`grep $1 binfiles/binscripts/Paths'
          mv ~/RecycleBin/$1 $restore
          sed -i '$ d' ~/binfiles/binscripts/Paths
      else 
         echo "That files doesn't exist.";
      fi

$1 represents the file, what I am trying to work out if $1 is moved back to its original directory and a file with the same name is already there how can i prompt the user to ask if they wish to overwrite it or not? and Paths is the text file the original directories are stored in. Thanks in advance
There are several things that could go wrong here:
  1. $!path might not do what you want if it doesn't appear at the start of the line in your 1st line of code.
  2. Having the 1st two lines of your script indicate that you want to use two different shells may confuse people trying to figure out what you're trying to do???
  3. What happens if $1 looks like an operator to test?
  4. What happens if $1 doesn't match any line or matches more than one line in binfiles/binscripts/Paths?
  5. What happens if binfiles/binscripts/Paths doesn't exist in the directory where this script is being run? (Note that you reference binfiles/binscripts/Paths twice and ~/binfiles/binscripts/Paths once. Should all three of them refer to ~/binfiles/binscripts/Paths?)
  6. What happens if the file you restore wasn't described by the last line in ~/binfiles/binscripts/Paths?
  7. Are all lines in ~/binfiles/binscripts/Paths just absolute pathnames of the file that is to be restored? What happens if a file is deleted, a new file with the same name is created, and then the new file is also deleted?
  8. What happens if there are any whitespace characters in one or more of your pathnames?
Assuming that none of the above cause you any problems, the following would print an error rather than restore the file if the destination path already exists:
Code:
          if [ ! -e "$restore" ]
          then    mv ~/RecycleBin/"$1" "$restore"
          else    printf "%s: \"%s\" already exists.\n" "$0" "$restore" >&2
                  exit 1
          fi

instead of:
Code:
          mv ~/RecycleBin/$1 $restore

Obviously, you can change the else clause to do whatever you want to prompt for permission to overwrite the existing file, use an alternative name, or whatever you else you decide you want to do.
# 3  
Old 10-21-2013
thanks alot for the help so far!

---------- Post updated at 02:47 AM ---------- Previous update was at 02:24 AM ----------

I have taken your points and added this to my script, how could implement a way of asking the user if they want to overwrite the file after they have seen the echoed message "already exists"

thanks

Last edited by zinn; 10-21-2013 at 10:46 PM..
# 4  
Old 10-21-2013
Quote:
Originally Posted by zinn
thanks alot for the help so far!

---------- Post updated at 02:47 AM ---------- Previous update was at 02:24 AM ----------

I have taken your points and added this to my script, how could implement a way of asking the user if they want to overwrite the file after they have seen the echoed message "already exists"

thanks
You're welcome.

So, what changes did you make to your script to address the points I raised earlier?

What OS are you using?

What shell are you using?
# 5  
Old 10-21-2013
Quote:
Originally Posted by zinn
I have taken your points and added this to my script, how could implement a way of asking the user if they want to overwrite the file after they have seen the echoed message "already exists"

thanks
How about setting up a function to prompt the user, similar to my post in this thread:

https://www.unix.com/shell-programmin...not-exist.html
# 6  
Old 10-21-2013
I am using Puppy Linux 5.3 and the shell is bash

and I replaced

Code:
mv ~/RecycleBin/$1 $restore

with

Code:
if [ ! -e "$restore" ]
          then    mv ~/RecycleBin/"$1" "$restore"
          else    printf "%s: \"%s\" already exists.\n" "$0" "$restore" >&2
                  exit 1
          fi

I am just trying to add onto the above so the user can choose if they wish to replace the file with a yes no using else

thanks
# 7  
Old 10-21-2013
That's what the posted link does they function has a couple of extra features that you will no doubt want (e.g. support for "Y" "yes" "N" or "no" answers, asking again if the user doesn't type a valid option)

Your code should look like this, put they yorn function definition somewhere near the top of your script then do:

Code:
if [ ! -e"$restore" ] || yorn "Destination file already exists. Replace"
then
    mv ~/RecycleBin/"$1" "$restore"
else
    exit 1
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Writing File Restore Script

Hi all, I have been tasked with creating a script that sends a file into a created "recycling" directory and another script that restores a "deleted" file. I have already created the removal script but am stuck on the restoring part. I need to restore the file to its original location by... (0 Replies)
Discussion started by: bashbeginner
0 Replies

2. Shell Programming and Scripting

Delete/Restore Script

Hi there is anyone able to show me two scripts for a delete and a restore command. Delete - move a file to a dustibin directory that i have created. check to see if that file exsits first in the dustbin if so delete the file before moving the new one. Restore - return the file to its... (1 Reply)
Discussion started by: arsenal1994
1 Replies

3. Shell Programming and Scripting

RecycleBin Script Restore

this is complete.. (2 Replies)
Discussion started by: zinn
2 Replies

4. Shell Programming and Scripting

Help with rsync file restore script

Hello Friends, I am trying to create an rsync file restore script. I will post want I have below and explain the problem I am having. #!/bin/bash # # Partial Rsync File Restore Script # # clear echo # Shell loads into /raid0/data/backup/bin/ cd .. # I cd to the backup... (1 Reply)
Discussion started by: jdavis_33
1 Replies

5. Homework & Coursework Questions

Bash Script Restore help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Attempting to make a auto restore script i have one made for backing up cron and installed programs just need to... (1 Reply)
Discussion started by: paperghost
1 Replies

6. Shell Programming and Scripting

Shell Script to save/restore files and dir permissions

Hello all: I need found script to save and restore file permissions and owner:group... this is for backup my server... I have one problem in my server and file permissions and owner:group, mess up.. For this reason i need found one way to secure this aspect of the server... Thanks! (1 Reply)
Discussion started by: joangopan
1 Replies

7. AIX

mksysb restore - Wrong OS level for restore

Hi all, I am still working on my mksysb restore. My latest issue is during an alt_disk_install from tape I got the following error after all the data had been restored. 0505-143 alt_disk_install: Unable to match mksysb level 5.2.0 with any available boot images. Please correct this... (0 Replies)
Discussion started by: pobman
0 Replies

8. Shell Programming and Scripting

not showing restore information while run script

Hello, I have the following script to restore file and grep information. However, once it restore file, it showing a lot useless information and different to check which file have the statement "John price $200". Can I not show any information while running script. It only show..when found the... (1 Reply)
Discussion started by: happyv
1 Replies

9. Shell Programming and Scripting

Restore Script

Hope I have posted this in the correct forum. I'm trying to recover some Oracle files from tape to setup an offsite recovery system. These files are nightly backdumps tar to tape. I have recovered rootvg and vg01 from the weekends full backup, now to restore these nightly files I discovered I... (1 Reply)
Discussion started by: Chrisp
1 Replies
Login or Register to Ask a Question