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.
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
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
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
./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
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