From: lbrtchx on 7 Feb 2010 14:24 is there a bash equivalent of \"this\" ... ~ referring to the current object's context? ~ I have some script that generates temporary error logs that are listed and deleted at the end if they are empty. I would like to go like: ~ find . -maxdepth 1 -name <...> -empty -cnewer <"this" running file> -exec ls -l {} \; ~ find . -maxdepth 1 -name <...> -empty -cnewer <"this" running file> -ok rm {} \; ~ I know where these temp files are and how their names are patterened, as well as that they must have been created later than the running piece of code, since they are created by it. ~ So, what is "this" for a bash script? ~ Thanks lbrtchx
From: Seebs on 7 Feb 2010 14:31 On 2010-02-07, lbrtchx(a)gmail.com <lbrtchx(a)gmail.com> wrote: > is there a bash equivalent of \"this\" ... > ~ > referring to the current object's context? No, because there's no "object" to be the current object or its context. > I know where these temp files are and how their names are patterened, as well as that they must have been created later than the running piece of code, since they are created by it. A few thoughts: 1. There's no file creation time. There's an inode change time, which is not necessarily the same thing. 2. This seems like borrowing unneeded complexity. If you want to know what the current script is, it's $0, and you have a pretty safe bet that either: 1. $0 is a qualified path, and is either an absolute path or a path relative to the current directory at the start of the script. or 2. $0 is a bare name, and is found in $PATH. .... "Pretty safe", BTW, is not a 100% guarantee. Anyway, it seems much simpler to just look for temporary files with names matching those patterns in the place where you are looking for them, which are empty, and delete those, because the worst case is you'll delete empty logs which should have been written by a previous invocation but somehow weren't, and it is probable that the existence of the empty log files is uninteresting. The other typical strategy would be to give them a name which includes some reasonably unique tag (say, starting time plus PID) and only remove ones with that tag. -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
From: mop2 on 7 Feb 2010 19:00 On Sun, 07 Feb 2010 17:24:56 -0200, <lbrtchx(a)gmail.com> wrote: > is there a bash equivalent of \"this\" ... > ~ > referring to the current object's context? > ~ > I have some script that generates temporary error logs that are listed and deleted at the end if they are empty. I would like to go like: > ~ > find . -maxdepth 1 -name <...> -empty -cnewer <"this" running file> -exec ls -l {} \; > ~ > find . -maxdepth 1 -name <...> -empty -cnewer <"this" running file> -ok rm {} \; > ~ > I know where these temp files are and how their names are patterened, as well as that they must have been created later than the running piece of code, since they are created by it. > ~ > So, what is "this" for a bash script? Well, seems "this object" you are thinking in it as current running script for the bash. In this case, as already said by Seebs , you have the var "$0" with script name e path to it. If you want only th script's name you get it with ${0##*/} and, obviously you can do: this=${0##*/} The size of a file you can get with: stat -c %s file For me is common start a script with: LOG=/tmp/${0##*/}.log If you like these concept of objects you can create a source to be called at beginning of some scripts, for example: .. /dir/myprefs The LOG example above is a possible candidate, because the caller will give the name to log file. In these source file, you can have a function to materialize your thoughts, for example: erase_empty_log(){ if [ `stat -c %s $LOG` -eq 0 ];then rm $LOG else ls -l $LOG fi } If you have more than one log, you need parametrize the function, but this is easy. You can also invert the things, and think in each script as a parameter to a master script, named, hummm, my_prefs, and it will initialize log file(s) and after the end of called script, it checks LOG files and erases empty files and lists non empty. Sorry, for my limited English, but I think you feel the idea and the great number of possibilities you have with shell too.
From: Albretch Mueller on 8 Feb 2010 10:45 Thank you guys! ~ "$0" did it! ~ this is how I now generally delete empty log files in my code ~ find . -maxdepth 1 -name ${_LOG_FL_PATTERN} -empty -cnewer $0 -exec ls -l '{}' \; ~ find . -maxdepth 1 -name ${_LOG_FL_PATTERN} -empty -cnewer $0 -exec rm '{}' \; ~ lbrtchx
|
Pages: 1 Prev: basic loop assistance Next: How to find files with the same name -- different case? |