Advertisement
ZhenyaDudko

Untitled

Nov 26th, 2022
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.13 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [[ ! -f ~/.trash.log ]]
  4. then
  5.   echo "No files in trash found"
  6.   exit
  7. fi
  8.  
  9. FILE_NAME=""
  10.  
  11. if [[ $# -ne 1 ]]
  12. then
  13.   echo "Expected one parameter - name of file to restore"
  14.   echo "Print name of file to restore: "
  15.   read FILE_NAME
  16. else
  17.   FILE_NAME=$1
  18. fi
  19.  
  20. while true
  21. do
  22.   if [[ "$FILE_NAME" == *\0* || "$FILE_NAME" == *\/* ]]
  23.   then
  24.     echo "Incorrect file name"
  25.     echo "Print name of file to restore: "
  26.     read FILE_NAME
  27.     continue
  28.   fi
  29.  
  30.   FILE_TO_RESTORE=""
  31.   LINK_NAME=""
  32.  
  33.   exec 3<&0
  34.  
  35.   line_counter=0
  36.   while read line
  37.   do
  38.     line_counter=$((line_counter+1))
  39.     CURRENT_LINK_NAME=$(echo $line | awk '{print $NF}')
  40.     CURRENT_FILE_NAME=${line% $CURRENT_LINK_NAME}
  41.    
  42.     if [[ $CURRENT_FILE_NAME == */$FILE_NAME ]]
  43.     then
  44.       echo "Do you want to restore this file: " $CURRENT_FILE_NAME "? Print Y/N or another to exit"
  45.       read answer <&3
  46.       case $answer in
  47.       "Y")
  48.         FILE_TO_RESTORE=$CURRENT_FILE_NAME
  49.         LINK_NAME=$CURRENT_LINK_NAME
  50.         break
  51.         ;;
  52.       "N") continue ;;
  53.       *) exit ;;
  54.       esac
  55.     fi
  56.   done < ~/.trash.log
  57.  
  58.   if [[ $FILE_TO_RESTORE == "" || ! -f ~/.trash/$LINK_NAME ]]
  59.   then
  60.     echo "No files found to restore"
  61.     echo "Print name of file to restore: "
  62.     read FILE_NAME
  63.     continue
  64.   fi
  65.  
  66.   FILE_DIRECTORY=${FILE_TO_RESTORE%$FILE_NAME}
  67.   if [[ -d "$FILE_DIRECTORY" ]]
  68.   then
  69.     if [[ -f "$FILE_TO_RESTORE" ]]
  70.     then
  71.       echo "File with this name already exists"
  72.       echo "Print name of file to restore: "
  73.       read FILE_NAME
  74.       continue
  75.     fi
  76.     echo "Restored file in original directory"
  77.     ln ~/.trash/"$LINK_NAME" "$FILE_TO_RESTORE"
  78.   else
  79.     echo "Original directory does not exist. Trying to restore file in home directory.."
  80.     if [[ -f ~/"$FILE_NAME" ]]
  81.     then
  82.       echo "File with this name already exists"
  83.       echo "Print name of file to restore: "
  84.       read FILE_NAME
  85.       continue
  86.     fi
  87.     echo "Restored file in home directory"
  88.     ln ~/.trash/"$LINK_NAME" ~/"$FILE_NAME"
  89.   fi
  90.  
  91.   rm ~/.trash/"$LINK_NAME"
  92.   sed -i "${line_counter}d" ~/.trash.log
  93.   break
  94. done  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement