[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: asking for a better way to implement this
From: |
Ken Irving |
Subject: |
Re: asking for a better way to implement this |
Date: |
Sun, 26 Sep 2010 10:56:07 -0800 |
User-agent: |
Mutt/1.5.20 (2009-06-14) |
On Sun, Sep 26, 2010 at 06:15:57PM +0200, Christopher Roy Bratusek wrote:
> Hi all,
>
> I'm writing a wrapper for rm, which does not let the file/directory be
> removed, if
> there's a .dirinfo file in the directory containing "NoDelete".
>
> (feel free to ask what that's all about.)
>
> This is what I have:
>
> xrm () {
>
> for path in $@; do
> if [[ $path == -* || $path == --* ]]; then
> local RMO+="$path"
> else
> basedir=$(echo "$path" | sed -e "s/$(basename path)//g")
> if [[ -e "$path"/.dirinfo && $(grep NoDelete
> "$path"/.dirinfo 2>/dev/null) != "" ]]; then
> echo "can not delete delete $path, it's
> protected"
> elif [[ -e ${basedir}/.dirinfo && $(grep NoDelete
> "$basedir"/.dirinfo 2>/dev/null) != "" ]]; then
> echo "can not delete delete $path, it's
> protected"
> else
> $(which rm) $RM_OPTS $RMO "$path"
> fi
> fi
> done
>
> }
>
> Now, I wanted to ask, if there's a more elegant/better way to implement this.
Style is a matter of taste, but I think this is equivalent (not tested):
xrm () {
for path in "$@"; do
test ${path:0:1} == - && local RMO+="$path " && continue
for try in "$path" "${path%/*}"; do
test -e "$try"/.dirinfo || continue
grep -q NoDelete "$try"/.dirinfo || continue
echo "can not delete $try, it's protected"
continue 2
done
$(which rm) $RM_OPTS $RMO "$path"
done
}
A few points:
Since you don't quote $@ there's probably no reason to quote $path.
Your RMO will have options concatenated with no space between them.
Your sed 's///g' might misbehave, e.g., xrm /tmp/home/tmp. The bash % expansion
only operates on the last pattern.
The -e option to sed seems to serve no purpose.
I'm guessing your $(which rm) is intended to avoid calling rm(), but maybe \rm
would do the same thing? No, that still calls the function... I'm not sure
how to do that.
This is the bug-bash list, maybe not the best place for this kind of thing...
Ken
- asking for a better way to implement this, Christopher Roy Bratusek, 2010/09/26
- Re: asking for a better way to implement this,
Ken Irving <=
- Re: asking for a better way to implement this, Dennis Williamson, 2010/09/26
- Re: asking for a better way to implement this, Christopher Roy Bratusek, 2010/09/27
- Re: asking for a better way to implement this, Greg Wooledge, 2010/09/27
- Re: asking for a better way to implement this, Chris F.A. Johnson, 2010/09/27
- Re: asking for a better way to implement this, Christopher Roy Bratusek, 2010/09/27