Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-09-2004, 03:53 PM   #1
LQ Newbie
Registered: Jul 2004
Posts: 1

Rep: Reputation: 0
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.
Old 07-09-2004, 04:59 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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?

Old 07-09-2004, 05:32 PM   #3
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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.
Old 07-09-2004, 05:55 PM   #4
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
passing escape characters along with lp command zita Linux - General 0 10-05-2005 02:58 PM
Variables and escape characters? flobadon Programming 4 09-10-2005 05:34 PM
Basic bach scripting BruceC Linux - Newbie 4 02-25-2005 10:31 AM
Escape Characters in linux shunraj Linux - Software 1 05-18-2004 04:21 PM
escape characters not escaping BobNz Linux - Software 2 04-09-2004 04:34 AM

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration