Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Find command work with a variable containing "*"?

Status
Not open for further replies.

6656

Programmer
Nov 5, 2002
104
0
0
US
Hello guys,

I have a problem to pass a variable ($optn) containing quoted wild card to FIND command. for exsample.

Code:
#$1=name 
#$2=*.log

if [  "$1" == "name" ]; then
    optn="-name '$2'"
else
    optn="-mtim +$2"
fi 
find . -type f $optn -ls
Please give your advise to make it work.
Thanks in advance.
 
Er...
There are a number of bugs in your code, but I'll leave those as an excercise.
However the code
Code:
$2="*.log"
find . -name "$2"
will work. Note the double quotes.

Ceci n'est pas une signature
Columb Healy
 
to make it work
what is not working ?
computer crash ? any error message ? unexpected behaviour ? ... ?

To trace the execution of your script use the set -x command.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for your speedy response.

to feherke, the "$optn" doesn't work. error msg
Code:
find: bad option -name '*.log'
find: path-list predicate-list

to columb, option -name or -mtime is determined by if...then..fi.

Below is the expected result return to be excuted:

find . -type f -name "*.log"
or
find . -type f -mtime +100

Thanks again.
 
6656

Use PHV's suggestion and run the script with the -x parameter. Either
Code:
ksh -x myscript.ksh
or amened the first line of your script to
Code:
#!/bin/ksh -x
Then, if you post the output we can see what's going on. feherke's suggestion should work, it does on my system. As to your
to columb, option -name or -mtime is determined by if...then..fi.

Below is the expected result return to be excuted:

find . -type f -name "*.log"
or
find . -type f -mtime +100
That's not what your code snippet does!

Ceci n'est pas une signature
Columb Healy
 
Here is full code:
Code:
 #!/bin/ksh -x
 #$1 - name of option
 #$2 - pattern of filename or age of file

optn=`echo "$1"| tr "[A-Z]" "[a-z]"`
if [ "$1" == "name" ]; then
    optn="-name '$2'"
else
    optn="-mtime +$2"
fi
find . -type f $optn -ls
output:
~/temp>rmf.sh name *.txt
+ rmf.sh name eli.txt ftpfile.txt section.txt test_syb.txt user.txt

There is no problem with " rmf.sh mtime 300" command

 
Call your script like this:
rmf.sh name "*.txt"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
Call your script like this:
rmf.sh name "*.txt"

output:
~/temp>rmf.sh name "*.txt"
+ rmf.sh name *.txt
+ + tr [A-Z] [a-z]
+ echo name
optn=name
+ [ name == name ]
+ optn=-name '*.txt'
+ find . -type f -name '*.txt' -ls

In above '*.txt', single quote is treated as part of tring, but not quote function.
 
Even if you replace this:
optn="-name '$2'"
with this ?
optn="-name $2"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
with this ?
optn="-name $2"
output:
Code:
~/temp>rmf.sh name "*.txt"
+ rmf.sh name *.txt
+ + tr [A-Z] [a-z]
+ echo name
optn=name
+ [ name == name ]
+ optn=-name *.txt
+ find . -type f -name eli.txt ftpfile.txt section.txt test_syb.txt user.txt -ls
find: bad option ftpfile.txt
find: path-list predicate-list
If it can be resulted in quotes around the filenames at final execute time, such as,
find . -type f -name "eli.txt ftpfile.txt section.txt"
 
What are you trying to do? Above you have $2=*.log then above you have optn=*.txt. What happened to *.log?

Anyway, as first mentioned there are too many problems with the code and it wouldn't run anyway.
 
To kHz:

The issue is how to fix the code to pass the paremater value with '*' in the script.

Something is weird that it causes the syntax error when assigning a value '*' or '*.log' to a parameter, but '*.txt', '*.abc', '*.log.*' or 'a*.log' is OK. Any idea why?

Thanks again
 
If the script fails when you pass '*' or '*.log' but not with other patterns then this nearly always means that you have a number of *.log files in your working directory
Consider the code
Code:
find . -name *.log
If there are no files called something.log then this will be passed to the shell unaltered and work as expected. Now, suppose that we have two files one.log and two.log, in this case what will be passed to the shell is
Code:
find . -name one.log two.log
which is a syntax error.

The problem you now mention is slightly different to how we understood the orignal post. Anyway the script
Code:
find . -name "$1"
works for me when called using
Code:
./testscript "*.log"
Note all the double quotes, both within and without the script.

Ceci n'est pas une signature
Columb Healy
 
Going through your code line by line
Code:
#!/bin/ksh -x
 #$1 - name of option
 #$2 - pattern of filename or age of file

no problems so far
Code:
optn=`echo "$1"| tr "[A-Z]" "[a-z]"`
You've set 'optn' to $1 translated to lower case, but thenyou never use this. However you then test $1 against 'name' I assume you want to test $1, translated to lower case against 'name'
# so
Code:
if [ "$1" == "name" ]; then
should be
Code:
if [ "$optn" == "name" ]; then
Code:
    optn="-name '$2'"
Well, er, '$2', because of the single quotes, is uninterpreted, and you want optn to be '-name *.log' so try
Code:
  optn="-name \"$2\""
Note the escaped double quotes
Code:
else
   optn="-mtime +$2"
fi
Not to much wrong here. You haven't checked that $2 is numeric but we'll live with that.
Finally
Code:
find . -type f $optn -ls
Whilst this produces the right command, for some reason it doesnt seem to work. I resolved this by changing
Code:
  optn="-name \"$2\""
to
Code:
  optn="find . -type f -name \"$2\" -ls"
and the final line to
Code:
ksh -c "$optn"

Putting it all together you get
Code:
#!/bin/ksh -x
 #$1 - name of option
 #$2 - pattern of filename or age of file
optn=`echo $1 | tr "[A-Z]" "[a-z]"`
if [ "$optn" = "name" ]
then
  optn="find . -type f -name \"$2\" -ls"
else
  optn="find . -type f -mtime +$2"
fi
ksh -c "$optn"
Which works for me.

Ceci n'est pas une signature
Columb Healy
 
Thanks a lot! Columb,

I think i'm going to take the codeing method as below.
Code:
if [ "$optn" = "name" ]
then
  find . -type f -name "$2" -ls
elif [ ...condition... ]; then
 ... more....
else
  find . -type f -mtime +$2 -ls
fi
It just seems more typing than the way in my original approach.
Thanks all again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top