find command from a shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users find command from a shell script
# 1  
Old 01-23-2008
find command from a shell script

Hi experts,
I have a shell script (korn shell on aix) where I am giving find command with file options which are read from a configuration file. For some reason I am getting an error find: 0652-017. I have put set -x in the shell script and the command looks okay. If I cut it and paste it in the dollar prompt the command works. I have spent so many hours trying to find out the problem, but no success. Here is the output from the shell

+ find /tmp -type f -a ! \( -name "*.Z" -o -name "*.gz" -o -name "DUMMY_*" -o -name "testfile_160108.gz" \) -a -name "testfile_1*" -a -mtime +0 -print
find: 0652-017 ! \( -name "*.Z" -o -name "*.gz" -o -name "DUMMY_*" -o -name "testfile_160108.gz" \) is not a valid option.

any idea why it works when pasted on the dollar prompt but not from within a shell ?
# 2  
Old 01-24-2008
Escape (\) !

Many times ESCAPE or \ has bit behaviroal difference when it used on shell-prompt and in shell-script. Following command will work for you

find /tmp -type f -a \! \( -name "*.Z" -o -name "*.gz" -o -name "DUMMY_*" -o -name "testfile_160108.gz" \) -a -name "testfile_1*" -a -mtime +0 -print
# 3  
Old 01-24-2008
find command from a shell script

Thank you very much for your help sumit. Escaping ! didn't work for some reason.

I will post the full code and the test data for better clairity. The purpose of the script is to zip files which are older taking parameters from a configuration file.

Here is the dat in the configuration file.

# * This File contains the data for performing
# * File housekeeping by the job housekeeping.sh.
# *
# * Each entry should have:
# * 1) Job Title
# * 2) Directory to be addressed (all sub-directories WILL be included)
# * 3) File name (* can be used as a wildcard)
# * 4) Mininum Number of files to retain (Deletion only)
# * 5) Age of the files (days) to be addressed
# * 6) Function - (C)ompress or (D)elete
# ************************************************************************
#
#JOB DIRECTORY FILE FILES AGE FUNC Exclude
#
#================================================================================================
#
HB02 /tmp testfile_1* 0 3 C DUMMY_*, testfile_1601*
~


Here is the full script

#!/bin/ksh
MODE=""
$MODE
set -x

grep -v '^#' test1.dat |
while read JOB DIRECTORY FILE MIN_FILES TIME FUNCTION EXCEPT
do

EXCEPT_STRING='\! \( -name "*.Z" -o -name "*.gz" -o '
con_flag=''
for exceptfile in `echo ${EXCEPT} | tr ',' ' ' `
do
EXCEPT_STRING="$EXCEPT_STRING ${con_flag} -name \"${exceptfile}\" "
con_flag='-o'
done
EXCEPT_STRING=$EXCEPT_STRING'\)'
echo 'Except string : = ' $EXCEPT_STRING
BEFORE=$(find $DIRECTORY -type f -a "$EXCEPT_STRING" -a -name \"$FILE\" -a -mtime +$TIME -print |wc -l)
done


Here is the output I am getting.
+ read JOB DIRECTORY FILE MIN_FILES TIME FUNCTION EXCEPT
+ grep -v ^# test1.dat
+ EXCEPT_STRING=\! \( -name "*.Z" -o -name "*.gz" -o
+ con_flag=
+ tr ,
+ echo DUMMY_*, testfile_160108.gz
+ EXCEPT_STRING=\! \( -name "*.Z" -o -name "*.gz" -o -name "DUMMY_*"
+ con_flag=-o
+ EXCEPT_STRING=\! \( -name "*.Z" -o -name "*.gz" -o -name "DUMMY_*" -o -name "testfile_160108.gz"
+ con_flag=-o
+ EXCEPT_STRING=\! \( -name "*.Z" -o -name "*.gz" -o -name "DUMMY_*" -o -name "testfile_160108.gz" \)
+ echo Except string : = \! \( -name "*.Z" -o -name "*.gz" -o -name "DUMMY_*" -o -name "testfile_160108.gz" \)
Except string : = \! \( -name "*.Z" -o -name "*.gz" -o -name "DUMMY_*" -o -name "testfile_160108.gz" \)
+ + wc -l
+ find /tmp -type f -a \! \( -name "*.Z" -o -name "*.gz" -o -name "DUMMY_*" -o -name "testfile_160108.gz" \) -a -name "testfile_1*" -a -mtime +3 -print
find: 0652-017 \! \( -name "*.Z" -o -name "*.gz" -o -name "DUMMY_*" -o -name "testfile_160108.gz" \) is not a valid option.
BEFORE= 0
+ read JOB DIRECTORY FILE MIN_FILES TIME FUNCTION EXCEPT
# 4  
Old 01-25-2008
Twick with double-quote and ESCAPE usage

Following piece of code works for me. This simply removes double-quote around EXCEPT_STRING in real find command, and remove un-wanted ESCAPE i.e. \ for EXCEPT_STRING creation

#!/bin/ksh
MODE=""
$MODE
set -x

grep -v '^#' test1.dat |
while read JOB DIRECTORY FILE MIN_FILES TIME FUNCTION EXCEPT
do

EXCEPT_STRING=' ! ( -name "*.Z" -o -name "*.gz" -o '
con_flag=''
for exceptfile in `echo ${EXCEPT} | tr ',' ' ' `
do
EXCEPT_STRING="$EXCEPT_STRING ${con_flag} -name \"${exceptfile}\" "
con_flag='-o'
done
EXCEPT_STRING=$EXCEPT_STRING')'
echo 'Except string : = ' $EXCEPT_STRING
BEFORE=$(find $DIRECTORY -type f -a $EXCEPT_STRING -a -name \"$FILE\" -a -mtime +$TIME -print |wc -l)
done
# 5  
Old 01-25-2008
Thanks a million

Sumit, thanks a million for your help. I spent so many hours trying to figure out. I can understand removing the backslash, but I am very curious to know how did you figure out the problem was with the double quotes and reason for it ?

Thanks once again
# 6  
Old 01-28-2008
Just an eagerness to help you!!! BTW its just trial and error with those special characters... As I mentioned in my first reply there are usual problems in Shell-Script with brackets (), back-space, ESCAPEs, and now quotes and double-quotes as well!!!
Also you can see what exactly happening with "sh -x shell-script-name.sh" to debug properly
# 7  
Old 01-28-2008
find didn't work eventhough no shell script error

Sumit, sorry to bother you. Eventhough the scripts works without any complaint, the find command really doesn't find the required files. If you get some time please can you have another look. I tried putting \ before the meta characters, but then it gives error while running the scripts.
You may touch or create those files in a directory to test it.

Thanks once again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Find command doesn't return in shell script.

Hi All, I am using below snippet to search for a string (read from a file 'searchstring.out') in all locations (/) and then iterate over the files found to write the locations and the respective owner to an output file. However, this doesn't work as I believe the find command doesn't exit's... (11 Replies)
Discussion started by: Vipin Batra
11 Replies

2. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

3. Shell Programming and Scripting

find command in shell script

Hi all, Please i need an explanation for the following statements ref_file=/tmp/cleanfiles export ref_file touch `TZ=WAT+2 date "+%Y%m%d%H%M"` $ref_file find . ! -name . -prune -type f ! -newer $ref_file -exec store_file.sh {} \; (1 Reply)
Discussion started by: anish_1982
1 Replies

4. UNIX for Dummies Questions & Answers

find command in shell script doesn't work

Hello all, Something strange going on with a shell script I'm writing. It's trying to write a list of files that it finds in a given directory to another file. But I also have a skip list so matching files that are in that skip list should be, well uhm, skipped :) Here's the code of my... (2 Replies)
Discussion started by: StijnV
2 Replies

5. Shell Programming and Scripting

Find command in Shell Script

hi I am a newbee in Shell scripting (hardly 7 days) I have to execute a shell script which looks like this #!/bin/sh var1=`date +"%Y%m%d"` echo $var1 find . -name "$var1*" -exec mv {} Delete/ \; the find command in the script is running independently but when kept in this script it is... (24 Replies)
Discussion started by: sweetnsourabh
24 Replies

6. Shell Programming and Scripting

find command in shell script

Hi, dirs.conf fine contains below data /a/b/c/dir1|50 /a/b/c/dir2|50 /a/b/c/dir3|50 In a shell script I do as below while read file_rec do dir_name=`echo "${file_rec}" | cut -d "|" -f 1` purge_days=`echo "${file_rec}" | cut -d "|" -f 2` if then... (3 Replies)
Discussion started by: icefish
3 Replies

7. Shell Programming and Scripting

Find command in Korn Shell

Hi, I am trying to execute the below in Ksh (telnet) find ./request.txt -mmin -30 It says find: bad option -mmin What i am trying to do is by using find command i am checking wheather the file request.txt is there for 30 minutes or not Please help (1 Reply)
Discussion started by: chinniforu2003
1 Replies

8. Shell Programming and Scripting

script to find whether shell command is available

I am running shell scripts on windows using Cygwin tool. In my shell scripts, i want to add an error check, that verify whether a certain command is available or not. For example if SED comamnd is not available in Cygwin, then it should exit with error message. How do i write such shell... (2 Replies)
Discussion started by: mmunir
2 Replies

9. UNIX for Dummies Questions & Answers

Shell script 'find' command

I have a script that has the following command: find /home/user -name test.dat The script works as desired when running normally. However, when I run the script preceding it with 'sh', it fails. Is there something I need to account for when preceding the execution of the script with 'sh'? (1 Reply)
Discussion started by: bsavitch
1 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