Advertisement
Guest User

Untitled

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