HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
cancel
Showing results for 
Search instead for 
Did you mean: 

Shell Script problem

 
Smucker
Regular Advisor

Shell Script problem

I have a find command that is being dynamically built then executed with a sub shell within a for loop.

The problem is the sub shell is single quoting all the escaped characters....

Below is a code snippet:

FINDPARM="\( -type f "

for SUFFIX in $(echo $PWDSRCH_EXCLUDE_FILE_SUFFIX|tr -s ',' ' ')
do
FINDPARM="$FINDPARM -a ! -name \"*.$SUFFIX \""
done

for EXCLUDE in $(echo $PWDSRCH_EXCLUDE_FILE|tr -s ',' ' ')
do
FINDPARM="$FINDPARM -a ! -name \"*$EXCLUDE* \""
done

FINDPARM="$FINDPARM \)"

GREPPARM=" -il"
for STRING in $PASSWDS
do
GREPPARM="$GREPPARM -e $STRING"

done



for FILE in `find $FINDROOT $FINDPARM -exec grep $GREPPARM {} \;`



The script errors out with the following display in debug (BTW - if I echo the command all looks good)
[28]: FINDPARM='\( -type f -a ! -name "*.so " -a ! -name "*.sl " -a ! -name "*.iso " -a ! -name "*.dbf " -a ! -name "*.tif " -a ! -name "*.dmp " -a ! -name "*.core " -a ! -name "*.html " -a ! -name "*.htm " -a ! -name "*.shtml " -a ! -name "*.db " -a ! -name "*.prn " -a ! -name "*redo* " \)'
[30]: GREPPARM=' -il'
[33]: GREPPARM=' -il -e trace'
[33]: GREPPARM=' -il -e trace -e test'
[33]: find / '\(' -type f -a ! -name '"*.so' '"' -a ! -name '"*.sl' '"' -a ! -name '"*.iso' '"' -a ! -name '"*.dbf' '"' -a ! -name '"*.tif' '"' -a ! -name '"*.dmp' '"' -a ! -name '"*.core' '"' -a ! -name '"*.html' '"' -a ! -name '"*.htm' '"' -a ! -name '"*.shtml' '"' -a ! -name '"*.db' '"' -a ! -name '"*.prn' '"' -a ! -name '"*redo*' '"' '\)' -exec grep -il -e trace -e test '{}' ';'
find: missing conjunction


Any Ideas

1 REPLY
James R. Ferguson
Acclaimed Contributor

Re: Shell Script problem

Hi:

This works:

#!/usr/bin/sh

FINDROOT="." #...added for testing...
FINDPARM="\( -type f "

for SUFFIX in pl txt zip #...modified for testing...
do
FINDPARM="$FINDPARM -a ! -name \"*.$SUFFIX\"" #...no trailing space...
done

for EXCLUDE in $(echo $PWDSRCH_EXCLUDE_FILE|tr -s ',' ' ')
do
FINDPARM="$FINDPARM -a ! -name \"*$EXCLUDE* \""
done

FINDPARM="$FINDPARM \)"

GREPPARM=" -il -e abort -e trace -e test" #...modified for testing...
#for STRING in $PASSWDS
#do
# GREPPARM="$GREPPARM -e $STRING"
#done

CMD="find $FINDROOT $FINDPARM -exec grep $GREPPARM {} +"
FILE=$(eval ${CMD})

for X in ${FILE}
do
echo ${X}
done

...

Regards!

...JRF...