LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2018, 08:36 AM   #1
DrFury
LQ Newbie
 
Registered: Aug 2018
Posts: 1

Rep: Reputation: Disabled
creating a recycling bin in Unix where user has the option to restore the file


Hi Everyone,

I am trying to create a recycle bin in unix where the user can o into the folder and have the opportunity to restore the file to its original folder.

I have created two vi: remove and restore23.

remove is working perfectly fine however the restore23 fail to execute its purpose.

below are the vi script for both:
remove
Code:
#!/bin/bash


#Global Variables

 recyclingBin=~/deleted
 restore=~/deleted/.restore.info



function validate()
{
if [ $# -eq 0 ]
   then
        echo "**Please provide the name of the file to restore**"

elif [ ! -e $recyclingBin/$1 ]
   then
        echo "**restore: File \`$1' doesn't exist**"

fi
}


function restore()
{
        abspath=$(grep $1 $restore | cut -d":" -f2)
        dir=$(dirname $abspath)
if [[ -d $dir ]]
   then
        mv $recyclingBin/$1 $abspath
        grep -v $1 $restore > temp && mv temp $restore  #removes the restored file record from .restore.info
else
        mkdir -p $dir
        mv $recyclingBin/$1 $abspath
        grep -v $1 $restore > temp && mv temp $restore
fi
}

function checkExist()
{
 abspath=$(grep $1 $restore | cut -d":" -f2)
if [ ! -e $abspath ]
   then restore $1

else read -p "Do you want to overwrite? y/n: " option

case $option in
        [Yy]*) restore $1 ;;
        *)         exit 0;;
        esac
        fi
}

validate $1
checkExist $1

******************************
restore23
Code:
#!/bin/bash


#Global Variables

 recyclingBin=~/deleted
 restore=~/deleted/.restore.info


#checking if the file exist

function validate()
{
if [ $# -eq 0 ]
   then
        echo "**Please provide the name of the file to restore**"
                exit 1
elif [ ! -e $recyclingBin/$1 ]
   then
        echo "**restore: File \`$1' doesn't exist**"
                exit 1
fi
}

#restore
function restore()
{
        abspath=$(grep $1 $restore | cut -d":" -f2)
        dir=$(dirname $abspath)
if [[ -d $dir ]]
   then
        mv $recyclingBin/$1 $abspath
        grep -v $1 $restore > temp && mv temp $restore  #removes the restored file record from .restore.info
else
        mkdir -p $dir
        mv $recyclingBin/$1 $abspath
        grep -v $1 $restore > temp  mv temp $restore
fi
}

#confirmation required
function checkExist()
{
        abspath=$(grep $1 $restore | cut -d":" -f2)
if [ ! -e $abspath ]
   then restore $1

else read -p "Do you want to overwrite? y/n: " option

case $option in
        [Yy]*) restore $1 ;;
        *)         exit 0;;
        esac

 fi
}

sed -i "/$1/d"~/deleted/.restore.info

validate $1
checkExist $1

*********************
everytime i run the sh restore23 command along a file in the deleted folder i get the error below:

Code:
$sh resorecopy try8_2802904
/bin/bash: resorecopy: No such file or directory
Can anyone help me figure out what am i doing wrong?

Many thanks

Last edited by astrogeek; 08-05-2018 at 01:27 PM. Reason: Added code tags
 
Old 08-05-2018, 09:35 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,863

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Please use code tags to keep formatting (of your script).
please use shellcheck (www.shellcheck.net) to check the script (and find some errors).
probably you just mistyped restore (resorecopy instead of restorecopy)
 
2 members found this post helpful.
Old 08-05-2018, 01:03 PM   #3
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Many distro's provide this kind of functionality automatically - if you use a GUI file manager.

Additionally, they cope with files with embedded spaces (which your scripts won't).
 
1 members found this post helpful.
Old 08-06-2018, 03:25 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
  • vi is an editor, not a scripting language. you are showing us bash scripts, not vi scripts.
  • the scripts seem somewhat advanced for someone asking so innocent questions. i wonder where you copy-pasted them from.
  • don't ask others to troubleshoot & debug your scripts for you. this is part of the process of learning how to script. learn it. start with 'set -x' at the top of your script.
  • '*************************' is not a comment in bash. it might cause weird behavior.
 
4 members found this post helpful.
Old 08-06-2018, 04:09 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> /bin/bash: resorecopy: No such file or directory

You don't have a script called resorecopy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
whether Linux as Restore option to Restore Previous Day Setting as Windows sanjay87 Linux - Newbie 2 11-12-2012 04:48 AM
AD Unix Attributes and Linux Fedora creating /home/user rgto Linux - Server 1 09-28-2012 03:15 AM
Creating System Restore Points in Linux - Unix devUnix Linux - General 5 02-22-2012 11:36 PM
LXer: Using Wget's User Agent Option Safely On Linux And Unix LXer Syndicated Linux News 0 01-06-2009 09:40 AM
Creating a log file in unix?? linux_dejan Programming 3 09-03-2004 10:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration