Advertisement
EVS10

Untitled

Dec 6th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.18 KB | None | 0 0
  1. #!bin/bash
  2.  
  3. # check if the input is correct
  4. if [[ "$1" =~ "/" ]]; then
  5.     echo "Input file can't contain '/' character"
  6.     exit
  7. fi
  8.  
  9. tmp=${HOME}/.tmp
  10. touch $tmp
  11. log_file=${HOME}/.trash.log
  12. mapfile -t array < $log_file
  13. file_was_found=false
  14. for line in "${array[@]}"; do
  15.     filename=$(echo "$line" | awk -F "/" '{ print $NF }') # { print $NF } prints the last column with delimeter "/"
  16.     # echo "Line $line"
  17.     # echo "Filename: $filename"
  18.     if [ "$filename" == "$1" ]; then
  19.         file_was_found=true
  20.         link_id=$(echo "$line" | awk '{ print $1 }')
  21.         line_length=`expr length "$line"`
  22.         link_id_length=`expr length "$link_id"`
  23.         let link_id_length_plus_two=$link_id_length+2
  24.         link_path=$(echo "$line" | cut -c$link_id_length_plus_two-$line_length)
  25.         echo "Would you like to restore the following file: $link_path? [y/n]"
  26.         read answer
  27.         case "$answer" in
  28.             y)
  29.                 file_length=`expr length "$1"`
  30.                 link_path_length=`expr length "$link_path"`
  31.                 let directory_length=$link_path_length-$file_length
  32.                 directory=$(echo "$link_path" | cut -c1-$directory_length)
  33.                 old_path=${HOME}/.trash/$link_id
  34.                 if [ -d "$directory" ]; then
  35.                     echo "Directory $directory exists"
  36.                     ln "$old_path" "$link_path"
  37.                     echo "The file was successfully restored into the directory $directory"
  38.                 else
  39.                     echo "Directory $directory does not exist"
  40.                     ln "$old_path" "${HOME}"
  41.                     mv "${HOME}/$link_id" "${HOME}/$filename"
  42.                     echo "The file was successfully restored into the ${HOME} directory"
  43.                 fi
  44.                 rm -f "$old_path"
  45.                 echo "The link in the trash was deleted"
  46.                 ;;
  47.             *)
  48.                 echo "The file $link_path will not be restored"
  49.                 echo "$line" >> $tmp
  50.                 ;;
  51.         esac
  52.     else
  53.         echo "$line" >> $tmp
  54.     fi
  55. done
  56. if [[ $file_was_found = false ]]; then
  57.     echo "File was not found!"
  58. fi
  59. cat $tmp > $log_file
  60. rm -f $tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement