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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting intro to UNIX - making a sort-of recycle bin (for fun)
# 1  
Old 04-27-2009
Bug 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

Code:
# if files exist, remove contents (of purge directory)
ls -lA ~/purge;
echo "^ this was a test, to be sure the following command should work"

purgeTest= "ls -lA ~/purge" # setting a variable to check contents of ~/purge
if [ $purgeTest -ne "total 0" ]; then
rm -r ~/purge/* 
fi

side note: I would think that checking if a directory (line 5) has contents is unnecessary, except when I just had rm -r ~/purge/* written, I'd get rm: cannot remove `/home/jzacsh/purge/*': No such file or directory BUT only when the purge directory was empty.

Here is the output I'm seeing on login:
Code:
total 0
^ this was a test, to be sure the following command should work
bash: ls -lA ~/purge: No such file or directory
bash: [: -ne: unary operator expected
jzacsh@dell8300:~$

bottom line, questions:
what am i doing wrong to get the "no such file or directory" error, in line 3 just above? (am I using variables incorrectly?)

is -ne operator only for mathematic comparisons - line 4 of error? if so, what should I use?

-thanks in advance for any suggestions!
# 2  
Old 04-27-2009
Why would you want the script to clean your "recylce bin" (doesn't GNOME have that by default?) in your login script? Lets assume that you, by accident "deleted" a file. As soon as you open a new terminal it's gone.

Aside from that, there are 3 errors in your code:
Code:
purgeTest= "ls -lA ~/purge"
if [ $purgeTest -ne "total 0" ]

First, there shouldn't be white spaces around the assignment operator. Second, double quotes won't capture the output of the command, use backticks or $( <cmd> ). Third, -ne is for numeric comparison, use != for strings.
# 3  
Old 04-27-2009
pludi: thanks for the quick reply. I actually wrote this originally for my Intro to Unix class, where I needed somewhere to stick temporary files I was working on/looking at, that I didn't want to worry about remembering to clean up. In that class we use FreeBSD. I fixed the errors you mentioned, thank you for the advice.

This is what my code looks like now:
Code:
# if files exist, remove contents of purge directory
purgeTest=`ls -lA ~/purge`
if [$purgeTest!="total 0"]; then
rm -r ~/purge/*
fi

Thisi is the error I get:
Code:
bash: [total: command not found

# 4  
Old 04-27-2009
Rewrite that if to
Code:
if [ "$purgeTest" != "total 0" ]; then

and everything should run as wanted.

As an additional learning challenge you might consider putting your code in your .bash_logout and only run it when your last terminal exits.
# 5  
Old 04-27-2009
Awesome, that worked! (I guess I got rid of the unnecessary whitespace and the necessary). Thanks.

I'm glad you mentioned that, I'm actually a bit confused with the different files. My UNIX teacher taught us ~/.profile was what you use, then the ubuntu forums explained you should use .bashrc for an assignment (like the one I did below).

Code:
alias test="cd $HOME/prog/test ; pwd ; ls -la"

My professor explained that its just because every linux/unix system is a bit unique, but I feel like there must be a more reasonable explanation for the different files - or at least a glossary/breakdown of the different ?daily-script-containers? somewhere ... no?
# 6  
Old 04-27-2009
Different shells, more likely. Nothing but BASH uses .bashrc . Lots of linux systems use BASH, but lots of other UNIX systems generally login to ksh or others. Though I have seen situations where a read-only .bashrc exists to prevent people mucking with it, that automatically includes .profile or somesuch if present.

Last edited by Corona688; 04-27-2009 at 09:30 PM..
# 7  
Old 04-28-2009
According to the man page of bash the sequence is
  1. /etc/profile
  2. ~/.bash_profile
  3. ~/.bash_login
  4. ~/.profile
plus anything that's sourced in there. ksh only reads /etc/profile and ~/.profile

Furthermore, bash will run ~/.bash_logout whenever a login shell exits, and ~/.bashrc when running an interactive non-login shell.
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. UNIX for Dummies Questions & Answers

UNIX fun stuff - echo and dc - obfuscate/garble a string sort of

Hi, Humorous UNIX Commands shows a fun way of using echo and dc to sort of obfuscate a string. % echo 'sasb3135071790101768542287578439snlbxq'|dc GET A LIFE! I am just wanting to know if there is a way to sort of use dc and echo to print out an obfuscated/garbled string instead... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

Recycle Bin

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

5. UNIX for Dummies Questions & Answers

Help with my recycle bin code

Hi~ I have a problem with my recycle bin code. #!/bin/bash if test !-d ~/.recyclebin #if recycle bin does not exists then mkdir ~/.recyclebin # then create recycle bin else mv $1 ~/.recyclebin #else move the deleted file in the recycle bin fi so when I... (10 Replies)
Discussion started by: zel2zel
10 Replies

6. 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

7. What is on Your Mind?

Indians making fun of Indians in a new TATA ad

This is a nice ad by Tata.... a2lPniSsFxs (1 Reply)
Discussion started by: Neo
1 Replies

8. 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

9. 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
Login or Register to Ask a Question