Restoring a file to its original location

Hello everyone,

I am attempting to make a recycling bin type application in shell script (tcsh). I have the whole part of the application done where someone can recycle files from one location to the recycling bin (the lower half of the program), this is not a problem. However I wanted to make another option if the user typed in something like recycle -cleanup as argument 1 it would go into a loop to check each file currently in the recycling bin and ask the user what to do. If the user chooses d, it deletes it. If they choose s it skips it, and lastly if they choose r I want to restore it. This is where the problem lies.

I am very inexperienced with shell programming in general so I am not even sure if the way I'm doing the -cleanup thing is right. But, is there a way to know the original file location so I can just put the file back? First check that there is not a file with a duplicate name in the old directory, then if not move it back. As of now I have no clue how to do this and any help would greatly be appreciated!

Thank you! This is what I have so far:

#!/bin/tcsh -f

if ["$1" == "-cleanup" ] then

set FILES=/$HOME/.garbage/*
set userChoice = " "
foreach file ( $FILES )
  echo "Processing $file file..."
  # take action on each file. $f store current file name

  echo "$file delete/restore/skip? (d/r/s)"
  set userChoice = $<

  if userChoice == "d" then
    rm $file
    echo "$file deleted!"
  else if userChoice == "r" then
   #restore to previous spot
   echo "$file restored!"
  else if userChoice == "s" then
   # do nothing
  else
   echo "You entered an invalid choice"

  endif
 
end
else


set n = 1
set size = 0
mkdir -p ~/.garbage

while ($n <= $#argv)
 if ( -d $argv[$n] ) then
   echo "$argv[$n] is a directory and cannot be removed"
 else
    
      
     mv $argv[$n] ~/.garbage
     
     echo "The file moved to the garbage directory was: "$argv[$n] "\n"
     set size = `du ~/.garbage | cut -f1`
  
 endif 
@ n++
end

  echo "The garbage bin's size is:" $size " bytes."
endif

---------- Post updated at 04:16 PM ---------- Previous update was at 03:54 PM ----------

Sometimes it is easier to put all the files or dirs deleted into one dir using sequential number names, and make a log of who is who. Then, you must make sure the restore target is there with something like 'mkdir -p' but the entry name is not occupied. It is heavy duty shell scripting and command usage.

This forum has a README:csh programming considered harmful
At least you should always quote "$var" when used as an argument (even in standard shells) and in set var2="$var" assignments.

rm "$file"
mv "$argv[$n]" ~/.garbage

You will experience! the difference if $file has the value * .
Better test this with echo:

echo $file
echo "$file"

When "quotes" cannot be used, the tcsh has the :q modifier but this often does not work either:

foreach file ( $FILES:q )

The script to put files from a certain area to the recycling bin already works. The one with the choice of d, r, or s does not. In this instance I am forced to use tsch because this is academic in nature. Personally, I don't know why tsch is even taught if Bash is supposed to be so much better. But I will keep trying and adjusting code. It seems like the only way to make a restore to the original location is to keep the path name somewhere in a log type file.

If your if ... then ... elsif ... then ... endif chain is about one variable, you can use a switch/case construct.
From man tcsh :

  set userChoice = $<
  switch ( "$userChoice" )
  case "d":
    rm "$file"
    echo "$file deleted!"
    breaksw
  case "r":
    #restore to previous spot
    echo "$file restored!"
    breaksw
  case "s":
    # do nothing
    breaksw
  default:
    echo "You entered an invalid choice"
  endsw

After reading man bash :

  read userChoice
  case "$userChoice" in
  "d")
    rm "$file"
    echo "$file deleted!"
    ;;
  "r")
    #restore to previous spot
    echo "$file restored!"
    ;;
  "s")
    # do nothing
    ;;
  *)
    echo "You entered an invalid choice"
  esac

With 'case', put your test patterns in full parens (...) so you do not sabotage the vi feature '%' for finding pairs or symmetrical container symbols.

  read userChoice
  case "$userChoice" in
  ("d")
    rm "$file"
    echo "$file deleted!"
    ;;
  ("r")
    #restore to previous spot
    echo "$file restored!"
    ;;
  ("s")
    # do nothing
    ;;
  (*)
    echo "You entered an invalid choice."
    ;;
  esac
/bin/sh -nx

syntax error: `(' unexpected.
For me portability is of greater concern than a vi feature.

Oh, not bourne compatible? Some sh are finicky? HPUX 11.00:

$ sh -c 'case 1 in
(?)
echo ok
;;
(*)
echo ng
;;
esac'
ok
$