I am creating a script to restore files back to their original location after being deleted and I keep having this error pop up. Why and how to fix the issue?

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

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

  if [[ $f ]]; then
  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"
  • side note: you should read mywiki.wooledge.org/ParsingLs – glenn jackman May 17 at 19:27
  • 1
    and here's a helpful site: shellcheck.net – Jeff Schaller May 17 at 19:28
  • btw, the error in your title appears truncated; it wouldn't hurt to edit the body and show an invocation of your script followed by the exact error message – Jeff Schaller May 17 at 19:28
  • I can’t not use shellscript. At the moment. I was trying to edit the body but my laptop is reacting crazy. Thanks for the information. I’m resetting my laptop now to fix the script. – Jeff Goldfish May 17 at 19:30

The error comes from the following line:

[[ $confirmation =~ ^[YyJj] ]] || { echo "Canceled"; exit }

If you add in a semicolon after the exit to indicate the end of the block, it should at least avoid the error that you were recieving:

[[ $confirmation =~ ^[YyJj] ]] || { echo "Canceled"; exit; }

That said, I am not a big fan of this type of notation, and I personally do not think it's necessary for a case like the one OP had posted. It's not that much harder to write out the full if/then/fi statement to avoid these type of syntax issues, plus writing it out would make the script easier to read, share, or debug in the future.

  • wow just a semi colon..... thank you so much I miss all the small details. I believe my code is still incorrect because it’s still not restore my deleted files so I’ll be going back to work :( – Jeff Goldfish May 17 at 19:36

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.