find command from shell scipt (ksh) problem


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users find command from shell scipt (ksh) problem
# 1  
Old 02-05-2008
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.
# 2  
Old 02-05-2008
Quote:
Originally Posted by kodermanna
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.
# 3  
Old 02-05-2008
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.
# 4  
Old 02-05-2008
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.
# 5  
Old 02-06-2008
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.
# 6  
Old 02-06-2008
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.
# 7  
Old 02-06-2008
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. How to Post in the The UNIX and Linux Forums

How to run the files in Linux shell scipt?

I have a homework and I tried to work on this but unable to find the solution. Can someone help me how to resolve the issue. I have a package file and it contains text file as prod.ame300_000001.101414145111.A.txt. In the text file it contains pdf file... (1 Reply)
Discussion started by: pavand
1 Replies

2. Shell Programming and Scripting

Regular Expression in Find command [KSH]

Hello, I am trying to use regex wtih find command in KSH. For some reason it is not working as expected. Input: comm_000_abc_0102.c comm_000_abc.c 456_000_abc_1212.cpp 456_000_abc_.cpp Expected Output: comm_000_abc_0102.c kkm_000_abc_8888.cpp (Basically I want to find all... (6 Replies)
Discussion started by: vinay4889
6 Replies

3. Shell Programming and Scripting

Problem with find command at ksh

Hi! I made a shell script which is offering menu choice. I made it on RHEL & then with little bit changes I was able to run successfully on AIX/ksh. Script is working fine with no issues other than a little one i.e., There is one choice in which I can list out and delete some files from a... (10 Replies)
Discussion started by: sukhdip
10 Replies

4. Shell Programming and Scripting

problems with ksh array and find command

set -A allfiles `find $usrhtml -type f` i am trying to populate this array with the find command. It works fine when find is looking through a single directory but when i add a new subdirectory the files in the subdirectory get duplicated. Can anyone help me and fix this so each files in... (1 Reply)
Discussion started by: bjhum33
1 Replies

5. Shell Programming and Scripting

Change the font of text in output file in shell scipt

hi, I want to change the font of text in output file. :( I tried the below code code: if awk 'BEGIN{if('$RSS'>='1000')exit 0;exit 1}' then RED=`echo "\033 i can see colors in terminal but not in output file :wall: please help me how i can get colors text in output file. edit... (1 Reply)
Discussion started by: sreelu
1 Replies

6. Shell Programming and Scripting

Help - Using Find command on dynamic files on KSH

Hi Forum. When I run the following find command, I get the desired results: find . \( -name a.out -o -name '*.o' -o -name 'core' \) -type f -ls I want for my script to dynamically calculate and assign a variable var1 to contain all the files that I want to search instead of hard-coding. ... (2 Replies)
Discussion started by: pchang
2 Replies

7. Shell Programming and Scripting

Problem with embedded FTP command in Ksh - System Cannot find the specified path.

I have the following FTP embedded in a Ksh script on AIX 5.3 ftp -n <<WHATEVER open 10.101.26.218 user hcistats ******* ascii put $thupdatefile put $thcollectfile quit WHATEVER Here is what my script returns: ... (3 Replies)
Discussion started by: troym72
3 Replies

8. Shell Programming and Scripting

help needed. run shell scipt remotely

Dear all , I have a script. this script called get.sh and can get some solaris infomation and save the result as result.tar.gz. the problem is : we have 12 servers. every time. I need to login 12 server and do the same job 12 times.:mad: master server ... (2 Replies)
Discussion started by: chinesefish
2 Replies

9. Shell Programming and Scripting

running db2 sql and exporting output from ksh scipt

Hi there, I am trying to write a shell script as root on AIX 5.3 where I change user to db2inst1, connect to our db2 database, run a sql select query and export the result of the query to a file. The code I have so far is as follows:- #!/usr/bin/ksh su - db2inst1 -c "db2 connect to... (0 Replies)
Discussion started by: candlino
0 Replies

10. UNIX for Advanced & Expert Users

Problem with find command in C-shell

when i use the following command find / -name '*.*' -exec grep -il 'text' {} \; I can redirect the errors to /dev/null. This happens only in ksh but not in csh. the 2>/dev/null is not working in csh. Can you some one suggest an alternative for this in csh ? (3 Replies)
Discussion started by: dhanamurthy
3 Replies
Login or Register to Ask a Question