LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 07-09-2004, 02:53 PM   #1
Bud-froggy
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.
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.
 
Old 07-09-2004, 03:59 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,964
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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
 
Old 07-09-2004, 04:32 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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, 04:55 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

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


Reply


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 01:58 PM
Variables and escape characters? flobadon Programming 4 09-10-2005 04:34 PM
Basic bach scripting BruceC Linux - Newbie 4 02-25-2005 09:31 AM
Escape Characters in linux shunraj Linux - Software 1 05-18-2004 03:21 PM
escape characters not escaping BobNz Linux - Software 2 04-09-2004 03:34 AM


All times are GMT -5. The time now is 10:48 AM.

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