The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find command from a shell script kodermanna UNIX for Advanced & Expert Users 6 01-28-2008 02:37 AM
Problem with find command when used with mtime arunkumar_mca UNIX for Dummies Questions & Answers 4 01-07-2008 08:37 PM
Problem with find command in C-shell dhanamurthy UNIX for Advanced & Expert Users 3 11-30-2006 01:16 AM
urgent: problem with find command Terrible UNIX for Advanced & Expert Users 10 06-07-2006 11:11 PM
Problem with find command nattynatty Shell Programming and Scripting 7 05-15-2002 09:16 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-05-2008
Registered User
 

Join Date: Jan 2008
Posts: 10
Stumble this Post!
find command from shell scipt (ksh) problem

Hi experts,

I have a simple shell script as follows.

#!/bin/ksh
FIND_STRING="\( -name 'testfile*.Z' -o -name 'DUMMY_*' \) "
find /tmp -type f $FIND_STRING -print

When I run this with ksh -x testscript, I get the following output.

+ FIND_STRING=\( -name 'testfile*.Z' -o -name 'DUMMY_*' \)
+ find /tmp -type f \( -name 'testfile*.Z' -o -name 'DUMMY_*' \) -print
find: 0652-009 There is a missing conjunction

If I cut and paste the second line of the output into the dollar prompt, it works fine. Donot know what is happening . I had posted a similar error few weeks before, but now I could pinpoint to a 2 liner.

Thanks in advance for your help.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-05-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 541
Stumble this Post!
Quote:
Originally Posted by kodermanna View Post
Hi experts,

I have a simple shell script as follows.

#!/bin/ksh
FIND_STRING="\( -name 'testfile*.Z' -o -name 'DUMMY_*' \) "
find /tmp -type f $FIND_STRING -print

When I run this with ksh -x testscript, I get the following output.

+ FIND_STRING=\( -name 'testfile*.Z' -o -name 'DUMMY_*' \)
+ find /tmp -type f \( -name 'testfile*.Z' -o -name 'DUMMY_*' \) -print
find: 0652-009 There is a missing conjunction

If I cut and paste the second line of the output into the dollar prompt, it works fine. Donot know what is happening . I had posted a similar error few weeks before, but now I could pinpoint to a 2 liner.

Thanks in advance for your help.
The variable FIND_STRING is not being expanded by the shell when the find is run and that's why when you replace it with its contents the find command runs alright.
Reply With Quote
  #3 (permalink)  
Old 02-05-2008
Registered User
 

Join Date: Oct 2007
Posts: 153
Stumble this Post!
Well, "$FIND_STRING" is being evaluated in that the variable name is being replaced with the variable contents. However, the contents themselves are not being evaluated...that would be double-evaluation, a 15-yard penalty.

You could either use the command 'eval' in front of the 'find' command, or just remove the backslashes when you set the string.
Reply With Quote
  #4 (permalink)  
Old 02-05-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 951
Stumble this Post!
Here is the ouput when you do set -x on the script

Code:
+ FIND_STRING=$'\\( -name \'testfile*.Z\' -o -name \'DUMMY_*\' \\)'
+ echo '\(' -name $'\'testfile*.Z\'' -o -name $'\'DUMMY_*\'' '\)'
\( -name 'testfile*.Z' -o -name 'DUMMY_*' \)
+ find /tmp -type f '\(' -name $'\'testfile*.Z\'' -o -name $'\'DUMMY_*\'' '\)' -print
find: \(: unknown option
If you change the find command line to

Code:
eval find /tmp -type f $FIND_STRING -print
things work as expected.
Reply With Quote
  #5 (permalink)  
Old 02-06-2008
Registered User
 

Join Date: Jan 2008
Posts: 10
Stumble this Post!
Thank you very much

Thanks a lot for your explanation and the eval works fine. I had not used eval before and didn't know it is so vital. I haven't fully digested the solution but will spend some time on it.

Thanks again for your kind help.
Reply With Quote
  #6 (permalink)  
Old 02-06-2008
Registered User
 

Join Date: Jan 2008
Posts: 10
Stumble this Post!
set -x output

pfmurphy, just wondered how did you get the output you had posted with set -x? In my system (ksh on aix) the output is same as I posted in the beginning.
Reply With Quote
  #7 (permalink)  
Old 02-06-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 951
Stumble this Post!
Basically eval causes a shell to parse a command line twice. The following simple example should help clarify things

Code:
name=PATH
echo $name
eval echo $name
eval echo $$name
eval echo \$$name
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0