Deleting Restoring And Empty Trash

1. The problem statement, all variables and given/known data:

A set of Linux shell scripts is required to allow users to ‘remove’ files without them really disappearing until the ‘dustbin’ is emptied. The three shell scripts required are :-

del <filename> - This script should move the file called <filename> (a full or relative pathname) to the dustbin directory. 

trash [-a] - This script should remove the contents of the dustbin directory. 

If the -a option is not used, the script should print the filenames in the dustbin one by one and ask the user for confirmation that they should be deleted. 

Otherwise, If the -a option is used, the script should simply remove ALL files from the dustbin. 

restore [-n] <filename> - This script should move the file called <filename> (a full or relative pathname) back to its original directory.

If the -n option is used, the script should allow the file to be moved to a directory nominated by the user.

2. Relevant commands, code, scripts, algorithms:

bash code
using ubuntu
I used a dustbin directory to keep my deleted files.

3. The attempts at a solution (include all code and scripts):

del script
 

Code:

#!/bin/bash
echo "Do you want to delete this file?"
echo "Y/N"
read ans
case "$ans" in
  Y) echo "`readlink -f $1`" >>/home/ian/store & mv $1 /home/ian/dustbin ;;
  N) echo "File not deleted" ;;
esac
trash script
 

Code:

#!/bin/bash

if ["$1" == "-a"]
then 
  cd /home/ian/dustbin
  rm -rf*
  cd /home/ian
  rm store
  touch store
else
  cd /home/ian/dustbin
  ls > dustbin
  for line in `cat dustbin`
  do
    echo "Do you want to delete?" $line
    echo "Y/N"
    read ans
    case "ans" in
      Y) rm $line;;
      N) "";;
    esac
  done
  rm dustbin
  cd
  rm store
  touch store
fi
restore script
 

Code:

#!/bin/bash
if [ "$1" == "-n" ]
then
  cd /home/ian/dustbin
  restore =`grep $2 /home/ian/store` 
  filename = `basename "$restore"` 
  echo "Where do you want to save to?"
  read location
  location1 = `readlink -f $location` 
  mv -i $filename "$location1"/$filename 
else
##if the restore script is run without the -n switch the file will be restored to its original location
  cd /home/ian/dustbin 
  restore = `grep $1 /home/ian/store` 
  filename=`basename "$restore"` 
  mv -i $filename $location 
fi

***********************************

Is there anything I can do to improve the scripts?

Suggestion:

Restore function should test to ensure file they are trying to restore is actually in the dustbin. 

Check for upper or lower case responses in your case statements (could also consider looping with a trap for non YorN answers):
 

Code:
     case $ans in
        Y|y) rm "$file";;
        N|n) ;;
        *) echo "Please answer Y or N" ;;

Couple of simplification tips:

To truncate a file you can use:
 

Code:

> store

Instead of piping ls to a tempfile and reading it back, try:

(also consider a renamed of variable to "file" to make things more self-documenting):
 

Code:

for file in *
do
   . . .
done

Have a Linux Problem
Linux Forum - Do you have a Linux Question?

Linux Books
Linux Certification, System Administration, Programming, Networking Books

Linux Home: Linux System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.