LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-31-2009, 04:37 PM   #1
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
[bash] alias + function = weird...


ok guys...

this is weird...
first the info...

$TRASH=/home/.trash

trash has:

/home/.trash
+ files/
+ info/
+ [bunch of files]

I am making a little script which has the following function:

Code:
clean()
{
find $TRASH/* -name 'files' -prune -o -name 'info' -prune -o -delete
find $HOME -name "*~" -delete
}
So basically i want to delete everything in trash EXCEPT those two folders.

I also have a nice alias:

Code:
alias exit="clean && exit"
Now comes the fun.

Code:
[~]$ ls $TRASH
files/ temp13  temp2   temp26  temp32  temp39  temp45  temp51   temp30
info/  temp58  temp64  temp70  temp77  temp83  temp9   temp9   temp 50

[~]$ clean

[~]$ ls $TRASH
files/  info/

[~]$
so it IS working as I want right?

now:

Code:
[~]$ echo $TRASH
/home/.trash

[~]$ ls $TRASH
files/   temp13  temp2   temp26  temp32  temp39  temp45  temp51 temp72
info/    temp14  temp20  temp27  temp33  temp4   temp46  temp52 temp59 

[~]$ exit
find: /home/.trash: No such file or directory
find: /home/.trash: No such file or directory
find: /home/.trash: No such file or directory
find: /home/.trash: No such file or directory
find: /home/.trash: No such file or directory
... [ad infinitum, until ^C]

[~]$ ls $TRASH
/bin/ls: cannot access find: /home/.trash: No such file or directory

[~]$
yes it deletes $TRASH!! (and as a little side effect it doesnt exit)

but why??

can somebody explain me the logic on that??

EDIT:Thanks for the corrections guys I had been too much on the computer, made some typos there. I just took a time out now im more awake!

Last edited by RaptorX; 07-31-2009 at 06:19 PM. Reason: full of typos!
 
Old 07-31-2009, 05:25 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Code:
find $HOME "*~" -delete
I think you wanted to delete files ending in "~" inside your $HOME directory, however this find command actually deletes all files inside $HOME as well as those inside the (probably non-existant) directory "*~". I hope you have backup . The command you want is
Code:
find $HOME -name '*~' -delete
Quote:
I also have a nice alias:

Code:
alias="clean && exit"
You seem to be missing the alias name here...
 
Old 07-31-2009, 05:25 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hello RaptorX
[QUOTE=RaptorX;3627069]$TRASH=/home/.trash
[snip]
Code:
[~]$ ls $TRASH
/bin/ls: cannot access /home/.Trash-1000: No such file or directory
It looks as if the value of $TRASH has changed from /home/.trash to /home/.Trash-1000. How can that be? If you can repeat the test, please try
Code:
echo "'$TRASH'"
before the ls $TRASH

Last edited by catkin; 07-31-2009 at 05:27 PM.
 
Old 07-31-2009, 06:25 PM   #4
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by ntubski View Post
I think you wanted to delete files ending in "~" inside your $HOME directory, however this find command actually deletes all files inside $HOME as well as those inside the (probably non-existant) directory "*~". I hope you have backup . The command you want is
Code:
find $HOME -name '*~' -delete

You seem to be missing the alias name here...
actually I have no missing files yet... :O
I fixed the post in both places thanks for pointing out!

@catkin

thanks for checking out this post!
Is true that the value of $TRASH was changed but even with the normal one is doing the same.


I guess it has to do with what you mentioned in the other post.
find is finding '.' and deleting it first... but i dont understand why when i pass the function alone it cleans normally as it should but when i make an alias with "clean && exit" suddenly it tries to delete '.'...

you are a good bash "translator" any ideas why this happens?!
 
Old 08-01-2009, 06:36 PM   #5
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
I discovered that the error was caused probably by bash itself since even though i was sourcing .bashrc who had the function and the alias it seems to be that bash was still using the old code which was trying to delete the '.' directory.

So it means that this command works fine:

Quote:
clean()
{
find $TRASH/* -name 'files' -prune -o -name 'info' -prune -o -delete
find $HOME -name "*~" -delete
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash alias - two words noir911 Linux - General 4 01-22-2009 09:13 AM
Bash looping or function within a function FirmbIT Programming 2 04-24-2006 01:06 PM
A bash alias question CodeWarrior Slackware 11 06-03-2003 07:09 PM
bash and alias kleptos Slackware 6 01-23-2003 08:01 AM
alias or function in .bashrc, which sends command to background ngomong Linux - General 1 04-23-2002 09:50 PM

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

All times are GMT -5. The time now is 09:05 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