I'm using FreeBSD and I understand and use the -prune
option on find
to prevent the command descending into a subdirectory. But I find the necessary syntax very clumsy and awkward. It also doesn't prevent the pruned dir itself showing up which is fixable but annoying.
I'd like to create a short script/alias which has the effect of adding a new -noenter
arg to the find
command if it's given before any other primaries (for simplicity).
The aim is that I can type either of:
find -xs /dir -noenter '$*' -name '*.conf' -ls
find -x -s /dir -noenter '$*' -name '*.conf' -ls
and it'll convert the args and execute:
/usr/bin/find -xs /dir \( -name '$*' -prune -or \( -name '*.conf' -and ! \( -type d -and -name '$*' \) \) \) -ls
/usr/bin/find -x -s /dir \( SNIPPED \) -ls
but I can also type as normal any routine "find" command and it'll pass it transparently to /usr//bin/find
to execute.
Logically the script needs to identify the first arg after the dir name, and then test if it's:
- ... equal to
-noenter
and followed by at least 2 more args (in which case I know how to build the arg I need to pass to/usr/bin/find
by knowing which args were before/after it); or - ... equal to
-noenter
but not followed by at least 2 more args (= error); or - ...anything else or dir was the last arg or wasn't found (= pass entire original args to
/usr/bin/find
unaltered).
I can do all of this except one thing - how do I code the part of the script that examines argv to tell which arg number (if any) is the dir
argument?
My choice of shell is sh
for scripting.