LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [bash] alias + function = weird... (https://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-alias-function-%3D-weird-744242/)

RaptorX 07-31-2009 04:37 PM

[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! :D

ntubski 07-31-2009 05:25 PM

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 :eek:. 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...

catkin 07-31-2009 05:25 PM

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

RaptorX 07-31-2009 06:25 PM

Quote:

Originally Posted by ntubski (Post 3627119)
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 :eek:. 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?! :D

RaptorX 08-01-2009 06:36 PM

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
}


All times are GMT -5. The time now is 03:58 AM.