Need Help to deal with empty string

Hi one to All

i have written the code for searching the string in the file for specified path. its working fine,

but my req:if the string is not available in the file , it should display the Message saying that , string is not available in the file.

code is:
echo "give the path where we can search the file and the string"
read path
echo -e "Give your inputs here, first filename, followed by the string with a space"
read -a file_string
echo "========================================================================"
echo -e "Here is your required output \n"
for i in `find "$path" -name "${file_string[0]}*"`;do cat $i | grep -i ${file_string[1]};done

Till now its working fine , but when the string is not available in the file it should display the message.

Thanks
Saic

-q option of the grep

grep -q "search_string" filename

if [ $? -eq 0 ]
then
  echo "search string found"
else
  echo "search string not found"
fi

What i'd do (and you may or may not be able to do this is) -
direct your "grep" to a file as well as to the standard output (do it twice)
and at the end of the "do", the following
if [ ! -s (whatever you called your redirect file) ]
then
echo "STRING NOT FOUND IN FILE"
fi

Hi
i have tryed this but its giving error?

echo "give the path where we can search the file and the string"
read path
echo -e "Give your inputs here, first filename, followed by the string with a space"
read -a file_string
echo "========================================================================"
echo -e "Here is your required output \n"
for i in `find "$path" -name "${file_string[0]}*"`;do cat $i | grep -q ${file_string[1]};done
if [ $? -eq 0 ]
then
echo "search string found"
else
echo "search string not found"
fi

Please post the error message that your are receiving. Also, please tell us which shell you are using. Bash? KSH?

that should be within the for loop

for i in `<some_command>`
do
cat $i | grep -q "search_string"
if [ $? -eq 0 ]
then
echo "search_string found"
else
echo "search_string not found"
fi
done

Hi matrixmadhan,

thanks for giving reply. iam using "Bash"

./srch0.sh
give the path where we can search the file and the string
/
-e Give your inputs here, first filename, followed by the string with a space
./srch0.sh[7]: read: bad option(s)

-e Here is your required output
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .
search string not found
grep: illegal option -- q

And the new code is :

echo "give the path where we can search the file and the string"
read path
echo -e "Give your inputs here, first filename, followed by the string with a space"
read -a file_string
echo "========================================================================"
echo -e "Here is your required output \n"
for i in `find "$path" -name "${file_string[0]}*"`
do
cat $i | grep -q ${file_string[1]}
if [ $? -eq 0 ]
then
echo "search string found"
else
echo "search string not found"
fi
done

what's the version of the grep being used?

( my reference ) -q option is available in GNU grep 2.5.1

Hi matrixmadhan,

i dont have much idea on the version, if u dont mind please tell how can fine the version of the GREP

basically iam oracle dev. u can understand my prob.

thanks
SAIC

Hi

With out _q option ,any other options for this.

thanks
SAIC

to find version of grep

grep --v

yes there is,

grep "search_string" filename 2>/dev/null 1>&2
if [ $? -eq 0 ]
then
echo "search_string found"
else
echo "search_string not found"
fi

Hi Matrix
i have tred follwing code, but it showing some error,please clarify me this.

echo "give the path where we can search the file and the string"
read path
echo -e "Give your inputs here, first filename, followed by the string with a space"
read -a file_string
echo "========================================================================"
echo -e "Here is your required output \n"
for i in `find "$path" -name "${file_string[0]}*"`
do
cat $i | grep -iq "${file_string[1]}" 2>/dev/null 1>&2
if [ $? -eq 0 ]
then
echo "search string found"
else
echo "search string not found"
fi
done

ERROR:./srch0.sh
give the path where we can search the file and the string
/
-e Give your inputs here, first filename, followed by the string with a space
./srch0.sh[7]: read: bad option(s)

-e Here is your required output
./srch0.sh[13]: syntax error at line 13 : `for' unmatched

thanks
SAic