LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bach scripting and escape characters... (http://www.linuxquestions.org/questions/linux-newbie-8/bach-scripting-and-escape-characters-203135/)

Bud-froggy 07-09-2004 02: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.
Code:

# 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
Code:

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 03:59 PM

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

Code:

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

Does this do what you expect?


Cheers,
Tink

Dark_Helmet 07-09-2004 04: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 04: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:
Code:

#!/bin/bash

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

# Prevent pathname expansion
set -f

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

# Restore pathname expansion
set +f

exit 0



All times are GMT -5. The time now is 06:45 AM.