-   Linux - Newbie (
-   -   bach scripting and escape characters... (

Bud-froggy 07-09-2004 03:53 PM

bach scripting and escape characters...
I am having some trouble with basck scripting...
I need to take a list of command line arguments and run a find on each of them. The problem is that some of them contains wild characters, and everytime I pass it to find, they are expanded according to the contents of the current directory.

# rmex "*.txt" -x "file*"
# put the first portion into the variable ${PATTERNS}
PATTERNS=`echo "$@" | awk 'BEGIN{ FS="-x " } {print $1 }'`
# debug says: PATTERNS=*.txt
for var in PATTERNS
  `find ./ -name "${var}" -print`
#but here, debug says: find ./ -name "file.txt file2.txt" -print

I tried escaping *, but then the last line looks like:
find ./ -name "\*.txt" -print

So, how can I pass it in and have it read the * correctly?
I tried

export TESTER="*.txt"
find ./ -name "${TESTER} -print
# debug says: find ./ -name "*.txt" -print

and that worked fine, but I can seem to apply that to my bash script.
help would be much appreciated.

Tinkster 07-09-2004 04:59 PM

I'm not 100% sure I understand what you're trying to achieve ...


PATTERNS="`echo "$@" | awk 'BEGIN{FS="-x "}  {print $1 }'`"
# debug says: PATTERNS=*.txt
for var in $PATTERNS
  find ./ -name "${var}" -print

Does this do what you expect?


Dark_Helmet 07-09-2004 05:32 PM

Shell wildcard expansion will occur in your for loop statement. You could try changing $PATTERN to "$PATTERN", but that causes problems of its own. For instance, if you use more than one wildcard mask on the command line (e.g. rmex "*.txt" "*.c"), the double quotes will pass "*.txt *.c" as a single value into var. I don't know anything off the top of my head as a way around that.

Dark_Helmet 07-09-2004 05:55 PM

Found what you need. There's a way to turn off pathname expansion. Use set -f to turn it off, and set +f to turn it back on. So, this should work for you:


PATTERNS=`echo "$@" | awk 'BEGIN{ FS="-x " } {print $1 }'`

# Prevent pathname expansion
set -f

for var in ${PATTERNS}
  find ./ -name "${var}" -print

# Restore pathname expansion
set +f

exit 0

All times are GMT -5. The time now is 12:55 PM.