dear friends
I have a wrote a shell script which works like this.
1.) a command is executed and the log is moved in the file.
2.) this file is copied in to the other file.
3.) used a grep command to find a particular word.
4.) if a particular word is there then the script will go to next step else it will sleep for 20 seconds.
5.) the source file which is now appended after sleep of 20 seconds will be copied the other file.
6.) the updated other file is read a particluar word is searched again.
the script is as shown below.
searchstring()
{
if [ `echo $req1 | grep -c "SUCCESS" ` -gt 0 ]
then
echo "Sleep 10"
else
echo "sleep 50"
sleep 50
cp dellog.txt dellog02.txt
searchstring
fi
}
cp dellog.txt dellog02.txt
req1=`grep SUCCESS dellog02.txt`
#echo $req1
searchstring
now after executing this sheel script the copied file is showing the word that i want but it is executing else part and the same step is getting repeated.
i hope you people will help me.
---------- Post updated at 10:14 AM ---------- Previous update was at 09:49 AM ----------
dear friends
i got the answer. i missed line req1=`grep SUCCESS dellog02.txt` after copy in function.
thank you
ravi
Hmmmm, TAKE GREAT CARE!!!
You are calling searchstring() inside itself...
Sooner or later it could crash out...
Also not sure where your "sleep 10" sits, certainly not inside the function.
A while :
or while true
might be a better way using shell _builtins_.
Dear wisecracker
Thank you for the reply. sleep 10 was just display something on the screen.
can you please help me by providing the code so that can call the function from outside unless my search gets the the wrd it wants.
Regards,
Ravi
You marked this thread as [SOLVED] using the title colour change.
Here is a idea that is totally untested so be aware of that.
Do NOT discard your current incarnation until this is proven...
# Shebang line here...
# Loop something like this...
# <Some unknown setup code here>
# IMPORTANT:- UNABLE TO TEST.
#
cp dellog.txt dellog02.txt
# req1=`grep SUCCESS dellog02.txt`
req1=$(grep SUCCESS dellog02.txt)
#
while true
do
# This section is added to exit the loop cleanly and manually...
char=""
# This line will sleep for 10 seconds UNLESS a key is pressed...
read -n1 -s -t10 char
# Q or q will exit/quit/breakout depending upon your requirement...
if [ "$char" == "Q" ] || [ "$char" == "q" ]
then
break
# exit # <some_return_code>
fi
# End of loop exit code...
# if [ `echo $req1 | grep -c "SUCCESS" ` -gt 0 ]
if [ $(echo $req1 | grep -c "SUCCESS") -gt 0 ]
then
# req1=`grep SUCCESS dellog02.txt`
req1=$(grep SUCCESS dellog02.txt)
echo "$req1"
# sleep 10
else
echo "Copying file(s)..."
cp dellog.txt dellog02.txt
# sleep 50
sleep 40
fi
done
#
echo "You have exited the continuous loop..."
Thank you wisecracker it is working now i have made some changes to make it simple. Regards, Ravi