Executing 'find' with variable as pattern problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing 'find' with variable as pattern problem
# 1  
Old 12-16-2009
Executing 'find' with variable as pattern problem

Hello everybody!
Here is my problem: I try to write a script that searches for files with several extensions using the find utility. The file extensions are defined in a list so I build a string (variable) of the pattern arguments with these extensions but can't get find working. Here is a code sample that is not working as it should:
Code:
#!/bin/bash
FILTER_PATTERN="-name \"*.o\""
echo $FILTER_PATTERN # -name "*.o"
#trying to execute find with this filter pattern
find -P ./ `echo -n $FILTER_PATTERN` -type -f

There is something fishy here in this last line since it doesn't show anything. Seems to be that the FILTER_PATTERN is not correctly understood by the interpreter. When I execute the command directly, it works fine.
Any idea for a solution?

PS tried the find -P ./ $FILTER_PATTERN -type f also but the same result.
# 2  
Old 12-16-2009
Code:
#!/bin/bash

FILTER_PATTERN='-name "*.o"'

eval "find -P ./ $FILTER_PATTERN -type f"

exit 0

Seems find has problems to substitute while executing so eval(uating) before execution does work for example.

Last edited by zaxxon; 12-16-2009 at 10:13 AM.. Reason: typo
# 3  
Old 12-16-2009
First, you've (probably) got a typo there, as it should be '-type f', instead of '-type -f'

Second, take a look at the trace of your script (with set -x):
Code:
+ FILTER_PATTERN='-name "*.o"'
+ echo -name '"*.o"'
-name "*.o"
++ echo -n -name '"*.o"'
+ find -P ./ -name '"*.o"' -type -f

You're telling find to look for files that start and end with double quotes. Drop them inside the first variable assignment, and everything should be fine.
# 4  
Old 12-16-2009
Thank you, I managed to get it work. The -x option helped a lot (I had forgotten about that).
I am not a pro in scripting, just trying to learn so might need some help from time to time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

4. Shell Programming and Scripting

To find ls of similar pattern files in a directory by passing the variable.

Hi, I have file in my $datadir as below :- SAT_1.txt SAT_2.txt BAT_UD.lst BAT_DD1.lst DUTT_1.txt DUTT_la.txt Expected result :- should get all the above file in $<Filename>_file.lst Below is my code :- for i in SAT BAT DUTT do touch a.lst cd $datadir (1 Reply)
Discussion started by: satishmallidi
1 Replies

5. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

6. UNIX for Dummies Questions & Answers

Find next line based on pattern, if it is similar pattern skip it

Hi, I am able to get next line if it is matching a particular pattern. But i need a way to skip if next line also matches same pattern.. For example: No Records No Records Records found got it Records found Now i want to find 'Records found' after 'No Records' pattern matches.. ... (5 Replies)
Discussion started by: nagpa531
5 Replies

7. Shell Programming and Scripting

find the line starting with a pattern and save a part in variable

Hi i have a file which has mutiple line in it. inside that i have a pattern similar to this /abc/def/hij i want to fine the pattern starting with "/" and get the first word in between the the symbols "/" i.e. "abc" in this case into a variable. thanks in advance (13 Replies)
Discussion started by: kichu
13 Replies

8. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

9. UNIX for Dummies Questions & Answers

Problem executing find file command in Linux

When trying to find a list of files with specific text in them using find . -type f -exec grep -l "DataStage Job 4263" {}\; I get error find: missing argument to 'exec' How can I correct this ? I'm on Linux Red Hat. Cheers PS I'm a DataStage programmer not a systems support... (4 Replies)
Discussion started by: jackdaw_at_work
4 Replies

10. Shell Programming and Scripting

Problem in executing a comand liying inside a variable

Hi everyone! I need some help with my shell script :( I am sending a shell command from a html text input to a cgi. Then, I store it into a variable. For example var="ps -axu" echo `$var` This functions properly. But consider the following... var="ps -axu | grep root" Now, I want... (2 Replies)
Discussion started by: Nene
2 Replies
Login or Register to Ask a Question