Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.92 KB | None | 0 0
  1. #!bin/bash
  2.  
  3. # check if the input is correct
  4.  
  5. echo "Dollar: $1"
  6. if [[ "$1" =~ "/" ]]; then
  7.     echo "Wrong input format!"
  8.     exit
  9. fi
  10.  
  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.         link_path=$(echo "$line" | awk '{ print $2 }')
  22.         echo "Would you like to restore the following file: $link_path? [y/n]"
  23.         read answer
  24.         case $answer in
  25.             y)
  26.                 file_length=`expr length "$1"`
  27.                 link_path_length=`expr length "$link_path"`
  28.                 let directory_length=$link_path_length-$file_length
  29.                 directory=$(echo "$link_path" | cut -c1-$directory_length)
  30.                 old_path=${HOME}/.trash/$link_id
  31.                 if [ -d "$directory" ]; then
  32.                     echo "Directory $directory exists"
  33.                     ln "$old_path" "$link_path"
  34.                     echo "The file was successfully restored into the directory $directory"
  35.                 else
  36.                     echo "Directory $directory does not exist"
  37.                     ln "$old_path" "${HOME}"
  38.                     echo "The file was successfully restored into the home directory"
  39.                 fi
  40.                 rm -f "$old_path"
  41.                 echo "The link in the trash was deleted"
  42.                 ;;
  43.             *)
  44.                 echo "The file $link_path will not be restored"
  45.                 echo "$line" >> tmp
  46.                 ;;
  47.         esac
  48.     else
  49.         echo "$line" >> tmp
  50.     fi
  51. done
  52. if [[ $file_was_found = false ]]; then
  53.     echo "File was not found!"
  54. fi
  55. cat tmp > $log_file
  56. rm -f tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement