Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!bin/bash
- # check if the input is correct
- # if [[ "$1" =~ "/" ]]; then
- # echo "Wrong input format!"
- # exit
- # fi
- touch tmp
- log_file=${HOME}/.trash.log
- mapfile -t array < $log_file
- file_was_found=false
- for line in "${array[@]}"; do
- filename=$(echo "$line" | awk -F "/" '{ print $NF }') # { print $NF } prints the last column with delimeter "/"
- # echo "Line $line"
- # echo "Filename: $filename"
- if [ "$filename" == "$1" ]; then
- file_was_found=true
- link_id=$(echo "$line" | awk '{ print $1 }')
- line_length=`expr length "$line"`
- link_id_length=`expr length "$link_id"`
- let link_id_length_plus_two=$link_id_length+2
- link_path=$(echo "$line" | awk '{ print $2 }')
- echo "Link path 1: $link_path"
- link_path=$(echo "$line" | cut -c$link_id_length_plus_two-$line_length)
- echo "Link path 2: $link_path"
- echo "Would you like to restore the following file: $link_path? [y/n]"
- read answer
- case $answer in
- y)
- file_length=`expr length "$1"`
- link_path_length=`expr length "$link_path"`
- let directory_length=$link_path_length-$file_length
- directory=$(echo "$link_path" | cut -c1-$directory_length)
- old_path=${HOME}/.trash/$link_id
- if [ -d "$directory" ]; then
- echo "Directory $directory exists"
- ln "$old_path" "$link_path"
- echo "The file was successfully restored into the directory $directory"
- else
- echo "Directory $directory does not exist"
- ln "$old_path" "${HOME}"
- echo "The file was successfully restored into the home directory"
- fi
- rm -f "$old_path"
- echo "The link in the trash was deleted"
- ;;
- *)
- echo "The file $link_path will not be restored"
- echo "$line" >> tmp
- ;;
- esac
- else
- echo "$line" >> tmp
- fi
- done
- if [[ $file_was_found = false ]]; then
- echo "File was not found!"
- fi
- cat tmp > $log_file
- rm -f tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement