1
#!/bin/bash
filename=../deleted/$1
#Testing condition before deletion of file
if [ "$1" = "" ] ; then
echo "No filename provided"
elif [  -f "../deleted/$1"  ] ; then
echo "File doesnot exist"
str=$(fgrep "$1" ../.restore.info | cut -d ":" -f2)
path=${str%/*}
mv "../deleted/$1" "${path}"
newname=$(fgrep "$1" ../.restore.info | cut -d "_" -f1)
mv -i "$1" "${newname}"
else
echo "file does not exist"
fi
----------

( I have written script to move file from the deleted folder to its original path and its working fine. But now i have to check if there is already a file with same name then it should give alert user "do u want to overwrite " if yes then overwrite if no or anything else then do not restore)

  • Question is already aswered in questions https://stackoverflow.com/questions/638975/how-do-i-tell-if-a-regular-file-does-not-exist-in-bash and https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script – Finwe Oct 15 '17 at 20:21

1 Answers1

0

Source: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

if [[ -e "$newname" ]]; then
   read -p "Overwrite? [y,N]" overwrite

   if [[ "$overwrite" = [Y,y] ]]; then
      mv -i "$1" "${newname}"
   fi
fi

The -e flag will simply check if it exists. (you can use the -f as you did above or -r to see if it exists and is readable.) The read command will prompt the user with the text in between the quotes and store it in the variable. The last if will only move the file if they input Y or y. (I didn't include it but you could easily add an else if to say not moved if they select no and also an else for an invalid response should you choose in the second if.)

(Not infront of my machine so I could not test it, but I am pretty sure all my syntax is correct. Let me know if not tho.)

sotI
  • 53
  • 1
  • 7