0

I have a script to recover files back to their original location. I am missing the options for the script to as "Do you want to recover this file?" Yes or No. How will I implement it into this code below Thanks!

#!/bin/bash
if [[ ! $1 ]]; then
echo -e "Usage:\n\n\t$0 'file name'"
exit 1
fi

f=$(ls 2>/dev/null -l /proc/*/fd/* | fgrep "$1 (deleted" | awk '{print $9}')

if [[ $f ]]; then
   echo "fd $f file found..."
cp -v "$f" "$1"
   else
echo >&2 "No file"
exit 2
fi
 }
1

Use read to ask for confirmation, e.g. like this:

echo "fd $f file found..."
read -p "Press enter to continue or Ctrl+c to cancel"
cp -v "$f" "$1"

or

echo "fd $f file found..."
read -p "Do you want to recover this file? [Yes/No] " confirmation
[[ $confirmation =~ ^[YyJj] ]] || { echo "Canceled"; exit }
cp -v "$f" "$1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.