Help with my recycle bin code


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with my recycle bin code
# 8  
Old 03-07-2011
another problem

Thanks for the help! It is now working. What I need now is to put [OPTIONS] for recovering the files from the recycle bin Smilie

Code:
$ rcvr -f filename

show the list inside the directory where the file was recovered (if the file is a regular file)
Code:
$ rcvr -h filename

show the list inside the directory where the file was recovered (if the file is a hidden file)

So this is the script with the getopt:

Code:
#!/bin/bash

set -- `getopt "fd:a" "$@"`

f= d=

while :
do
       case "$1" in
       -f) shift;
               cd ~/recovered_files | ls -l ;;
       -d) shift;
               cd ~/recovered_files | ls -al ;;
       --) break;;
       esac
shift
done

if [ -f ~/.recyclebin/$1 ] ;

then

mv  ~/.recyclebbin/$1 ~/recovered_files/$1

else

echo "No file exists"

fi


But when I tried
Code:
 $ rcvr -f filename

But the home directory is listed not the recovered_files Smilie(

What is wrong with my script?
# 9  
Old 03-07-2011
I don't know why it's not printing, it certainly at least runs ls for me. But then it runs into an infinite loop because of the extra 'shift' in your case statement. It eats the "-f", and outside of the case statement, the next shift eats your "--" argument, so you never reach the 'break'.

The -d option does need the shift because it does take an extra argument, and is safe because getopt will put nothing but "--" in your parameter list if you don't give it a parameter.

You should have while [ "$#" -gt 0 ] to prevent an infinite loop in case something eats your "--" argument. You can still break the loop whenever you please.

I don't understand your cd ~/whatever | ls -a cd prints no output to stdout, and ls reads no input from stdin -- there's nothing to pipe! As a side-effect ls gets created before cd happens and just lists the current directory.

Did you mean ls -a ~/whatever? This also avoids changing the directory at all.

Is your program meant to quit after -f? If so, you can just put an exit statement after the ls.

Last edited by Corona688; 03-07-2011 at 05:01 PM..
This User Gave Thanks to Corona688 For This Post:
# 10  
Old 03-07-2011
Thanks to that the ls part now works. The only problem is that the file in the recycle bin directory doesn't move into the recovered_files directory.

Here's the script:

Code:
#!/bin/bash

eval set -- `getopt f:h: "$@"`

while [ $# -gt 0 ]
do
        case "$1" in
                -f)     shift ;
                        ls -l recovered_files
                        exit
                        ;;
                -h)     shift ;
                        ls -al recovered_files
                        exit
                        ;;


                --) break
                        ;;
        esac
        shift
done


#move the file from the recycle bin -- to --- > recovered_files directory

if [ -f ~/.recyclebin/$1 ] ;

then

mv  ~/recyclebin/$1 ~/recovered_files/$1


else

echo "No file exists"

fi

It lists the recovered_files directory, but the file that is supposed to be in the recovered_files directory is not listed there. So the moving of file from the recycle bin to recovered_files directory didn't work right. Is the $1 in the if statements correct? How can I combine the getopt script with the recycble bin script?

---------- Post updated at 10:55 PM ---------- Previous update was at 08:17 PM ----------

Wah! The script is now working!

Here:

Code:
#!/bin/bash

eval set -- `getopt f:h: "$@"`

while [ "$#" -gt 0 ]
do
        case "$1" in
                -f)
                        if [ -f ~/.junkbin/$2 ] ;
                        then
                                mv ~/.junkbin/$2 ~/recovered_files/$2
                                echo " File is successfully recovered "
                                ls -l recovered_files
                        else
                                echo "File $2 does not exists "
                        fi
                        exit
                        ;;
                -h)     if [ -f ~/.junkbin/$2 ] ;
                        then
                                mv ~/.junkbin/$2 ~/recovered_files/$2
                                echo " Hidden file is successfully recovered "
                                ls -al recovered_files
                        else
                                echo "File $2 does not exists "
                        fi
                        exit
                        ;;


                --) break
                        ;;
        esac
        shift
done

I was planning to create a function so that I will not repeat the if [..] part anymore. but I dont know how to create a function. I think this script is enough already. Thanks Corona688 for your help! Godbless!

Last edited by zel2zel; 03-07-2011 at 11:28 PM.. Reason: Just encountered another problem
# 11  
Old 03-08-2011
Okay. If you ever decide you want to try functions:
Code:
#!/bin/bash
function functionname
{
        local VAR # A variable that won't overwrite the global variable VAR
        echo "First argument is $1"
        echo "Second argument is $2"
}

functionname "first" "second"

This User Gave Thanks to Corona688 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recycle bin.

Hi. I've created scripts for a recycle bin that can list, restore and empty it. I only have the problem of deleting two files with the same name. When I do it one file overwrite the other. What could I do to resolve it? The only thing I can think is asking the user to rename file before moving to... (2 Replies)
Discussion started by: ReonarudoB
2 Replies

2. UNIX for Dummies Questions & Answers

Recycle bin on minix 3.2.1?

Hi. I'm started to use minix 3.2.1 recently and I'm trying to create a recycle bin for it. I'm kinda struggling on how to do it. I searched internet and I found scripts created for it but I actually didn't learn how to create scripts in college and I'm not sure if I understand them. I just wanted... (1 Reply)
Discussion started by: ReonarudoB
1 Replies

3. Shell Programming and Scripting

Recycle Bin

what is recycle bin mode in unix??? (4 Replies)
Discussion started by: arun508.gatike
4 Replies

4. OS X (Apple)

When to use /Users/m/bin instead of /usr/local/bin (& whats the diff?)?

Q1. I understand that /usr/local/bin means I can install/uninstall stuff in here and have any chance of messing up my original system files or effecting any other users. I created this directory myself. But what about the directory I didn't create, namely /Users/m/bin? How is that directory... (1 Reply)
Discussion started by: michellepace
1 Replies

5. Homework & Coursework Questions

UNIX Recycle Bin - restore function

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: A set of Linux shell scripts is required to allow users to ‘remove' files without them really disappearing until... (8 Replies)
Discussion started by: burn88
8 Replies

6. Shell Programming and Scripting

Recycle Bin Script

Hello, I have having problems with an assignment and am pretty desperate. My assignment is to create a shell script that does a Recycle_Bin tasks. You can only open this with PuTTY software or Knoppix. Perhaps on other software that are able to read linux language. My part is stuck... (2 Replies)
Discussion started by: chueu
2 Replies

7. Shell Programming and Scripting

intro to UNIX - making a sort-of recycle bin (for fun)

Hello, I'm only taking Intro to UNIX in school right now, so please bear with me. My problem is with a sort-of recycle-bin rig I've created for fun. I'm using Ubuntu 9.04, I am the admin. (only user, actually) of this computer. I'm using this script in ~/.bashrc # if files exist, remove contents... (6 Replies)
Discussion started by: jzacsh
6 Replies

8. Windows & DOS: Issues & Discussions

Path of Recycle Bin on Windows

hello everybody, I am trying to find the path of the Recycle Bin. I know that it's a temporary storage place, but it should have a path that we can refer to. I want to know it because I sometimes use cygwin to work on Windows, and when you delete something with it, it's gone. I just checked... (4 Replies)
Discussion started by: milhan
4 Replies

9. Linux

how to view the code form /bin/sh

How do we view th ecode for executables?? qwk,grep.fgrep, etc.. any ideas?? as they are written in C , I think , there should be some place where they are placed. (2 Replies)
Discussion started by: singh85
2 Replies
Login or Register to Ask a Question