tail -XXX with grep doesn't work in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting tail -XXX with grep doesn't work in while loop
# 1  
Old 04-06-2010
tail -XXX with grep doesn't work in while loop

Hi all,

I need some help. my shell script doesn't work especially in the loop.

Code:
#!/bin/sh -xv

export ORA_ADMIN=/oracle/home/admin
export ORACLE_SID=ORA_SID
cat ${ORA_ADMIN}/param_alert_log.ora | while read MSG
do
  #echo $MSG
  #echo "tail -400 ${ORA_ADMIN}/${ORACLE_SID}/bdump/alert_${ORACLE_SID}.log \| grep $MSG"
  tail -400 ${ORA_ADMIN}/${ORACLE_SID}/bdump/alert_${ORACLE_SID}.log | grep $MSG >> /tmp/ora600_${ORACLE_SID}.ora
  #echo "Retour val : $?"
done

check_stat=`cat /tmp/ora600_${ORACLE_SID}.ora|wc -l`;
oracle_num=`expr $check_stat`
if [ $oracle_num -ne 0 ]
then
   newm=`diff /tmp/ora600_${ORACLE_SID}.ora /tmp/ora600_${ORACLE_SID}.old|wc -l`
   chgflg=`expr $newm`
   if [ $chgflg -ne 0 ]
   then
      cat /tmp/ora600_${ORACLE_SID}.ora | mailx -s "$ORACLE_SID : alerte(s) ORA- detectee(s)" admin@XXX.fr
      echo "envoi mail"
   fi
fi

cp /tmp/ora600_${ORACLE_SID}.ora /tmp/ora600_${ORACLE_SID}.old
rm -f /tmp/ora600_${ORACLE_SID}.ora


Many thanks for help

Best regards

Last edited by sidobre; 04-06-2010 at 06:44 AM.. Reason: code tags, please...
# 2  
Old 04-06-2010
put a $ sign before the ORA_SID
Code:
export ORACLE_SID=ORA_SID

should be
Code:
export ORACLE_SID=$ORA_SID

# 3  
Old 04-06-2010
Hi Panyam,

All environment variables are OK when I execute the script in debug mode. ORA_SID is the instance name like PROD or DEV...I've put the ORA_SID in the script just for not putting the true name.

The problem is if I execute the loop while, grep couldn't find the error in log file. grep -i also doesn't work. But if I execute individually the command below :
Code:
tail -400 ${ORA_ADMIN}/${ORACLE_SID}/bdump/alert_${ORACLE_SID}.log | grep ORA-XXXXX >> /tmp/ora600_${ORACLE_SID}.ora

It works.

Best regards

Abdel
# 4  
Old 04-06-2010
Can you try by echoing the "MSG" variable , i don't think it's getting proper value , hence the "grep" is failing.

Also make sure that there are no leading/trailing spaces in the "MSG" variable value.(This u can check by printing the length of the MSG field).
# 5  
Old 04-06-2010
Hi Panyam,

Yes I've echoed MSG and return $? fro grep. Below, the result execution on debug mode :
Code:
#!/bin/sh -xv
#***********************************************************
# recherche alerte ORA-600 et autres erreurs ORA-
# la liste des ORA- est dans parm_alert_log.ora
#***********************************************************
export ORA_ADMIN=/oracle/home/admin
+ export ORA_ADMIN=/oracle/home/admin
+ ORA_ADMIN=/oracle/home/admin
export ORACLE_SID=GESDOS
+ export ORACLE_SID=GESDOS
+ ORACLE_SID=GESDOS
#for MSG in `cat ${ORA_ADMIN}/param_alert_log.ora`
cat ${ORA_ADMIN}/param_alert_log.ora | while read MSG
do
  echo $MSG
  echo "tail -400 ${ORA_ADMIN}/${ORACLE_SID}/bdump/alert_${ORACLE_SID}.log \| grep $MSG"
  tail -400 ${ORA_ADMIN}/${ORACLE_SID}/bdump/alert_${ORACLE_SID}.log | grep $MSG >> /tmp/ora600_${ORACLE_SID}.ora
  echo "Return val grep : $?"
done
+ cat /oracle/home/admin/param_alert_log.ora
+ read MSG
+ echo $'ORA-00600\r'
ORA-00600
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-00600
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-00600
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ grep $'ORA-00600\r'
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG
+ echo $'ORA-1631\r'
ORA-1631
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-1631
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-1631
+ grep $'ORA-1631\r'
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG
+ echo $'ORA-1650\r'
ORA-1650
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-1650
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-1650
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ grep $'ORA-1650\r'
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG
+ echo $'ORA-1652\r'
ORA-1652
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-1652
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-1652
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ grep $'ORA-1652\r'
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG
+ echo $'ORA-1653\r'
ORA-1653
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-1653
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-1653
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ grep $'ORA-1653\r'
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG
+ echo $'ORA-00447\r'
ORA-00447
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-00447
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-00447
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ grep $'ORA-00447\r'
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG
+ echo $'ORA-00603\r'
ORA-00603
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-00603
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-00603
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ grep $'ORA-00603\r'
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG
+ echo $'ORA-01092\r'
ORA-01092
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-01092
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-01092
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ grep $'ORA-01092\r'
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG
+ echo $'ORA-02050\r'
ORA-02050
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-02050
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-02050
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ grep $'ORA-02050\r'
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG
+ echo $'ORA-1535\r'
ORA-1535
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-1535
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-1535
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ grep $'ORA-1535\r'
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG
+ echo $'ORA-02236\r'
ORA-02236
' echo 'tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-02236
tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log \| grep ORA-02236
+ tail -400 /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
+ grep $'ORA-02236\r'
+ echo 'Return val grep : 1'
Return val grep : 1
+ read MSG

#*************************************************************
# si erreur, envoi email . . . .
#*************************************************************
check_stat=`cat /tmp/ora600_${ORACLE_SID}.ora|wc -l`;
cat /tmp/ora600_${ORACLE_SID}.ora|wc -l
++ cat /tmp/ora600_GESDOS.ora
++ wc -l
+ check_stat=0
oracle_num=`expr $check_stat`
expr $check_stat
++ expr 0
+ oracle_num=0
if [ $oracle_num -ne 0 ]
then
   newm=`diff /tmp/ora600_${ORACLE_SID}.ora /tmp/ora600_${ORACLE_SID}.old|wc -l`
   chgflg=`expr $newm`
   if [ $chgflg -ne 0 ]
   then
      #*************************************************************
      # si erreur , envoi mail
      #*************************************************************
      #cat /tmp/ora600_${ORACLE_SID}.ora | mailx -s "$ORACLE_SID : alerte(s) ORA- detectee(s)" admin@site.fr
      echo "envoi mail"
   fi
fi
+ '[' 0 -ne 0 ']'

cp /tmp/ora600_${ORACLE_SID}.ora /tmp/ora600_${ORACLE_SID}.old
+ cp /tmp/ora600_GESDOS.ora /tmp/ora600_GESDOS.old
# rm -f /tmp/ora600_${ORACLE_SID}.ora

My alert log for debugging is :
Code:
cat /oracle/home/admin/GESDOS/bdump/alert_GESDOS.log
ORA-00600 : #### test error ####
ORA-1631 : #### test2 ####

Thanks

Last edited by sidobre; 04-06-2010 at 12:14 PM..
# 6  
Old 04-06-2010
There's a carriage return at the end of $MSG, which I assume is not there when you run the command directly.

---------- Post updated at 11:31 AM ---------- Previous update was at 11:27 AM ----------

If the carriage returns only occur as part of CPM/DOS/Windows style line endings, you can just throw them away with:
Code:
tr -d \\r < ${ORA_ADMIN}/param_alert_log.ora | while read MSG

# 7  
Old 04-06-2010
Also try putting quotes around the MSG variable:

Code:
grep "$MSG"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep doesn't work when assigning to variable

Hello, First of all, I'd like to say hello to all members of forum. Can You please help me with the matter described below? I am trying to fetch a data from the file to variable, I am doing this using below script: returned=`tail -50 SapLogs.log | grep -i -E "Error|"` echo $returned ... (2 Replies)
Discussion started by: jedzio
2 Replies

2. Shell Programming and Scripting

Why doesn't "grep -w" ALWAYS work?

Working with IP addresses is a pain... Here's my problem: I'm putting some interface information to a file: 3.185.201.2 | Tu1102 152.162.54.130 | Gi0/2.36 3.185.226.2 | Gi0/1 3.185.0.82 | Tu1 ... (12 Replies)
Discussion started by: turk22
12 Replies

3. Shell Programming and Scripting

pipe to grep doesn't work in bash script

Hi, I'm trying to write a script that checks gvfs to see if a mount exists so I can run it from network-manager's status hooks. I thought I'd pipe the output of gvfs-mount -l to grep for the particular mounts I care about. When I do this in a bash script: cmnd="gvfs-mount -l | grep -i... (4 Replies)
Discussion started by: kcstrom
4 Replies

4. Shell Programming and Scripting

Script doesn't work in loop but does if not

I have a script that only works if I remove it from the looping scenario. #!/bin/bash # Set the field seperator to a newline ##IFS=" ##" # Loop through the file ##for line in `cat nlist.txt`;do # put the line into a variable. ##dbuser=$line echo "copying plugin..." ... (6 Replies)
Discussion started by: bugeye
6 Replies

5. Shell Programming and Scripting

two grep in one script doesn't work?

Hi there, the following script doesn't work. the first part works, then the second 'grep' fails with ': not found'. However, if I take out the second part (starting with the grep command) and put in a seperate script, it works. everyone know what's wrong here? no two 'grep' in one script, that... (2 Replies)
Discussion started by: monkey77
2 Replies

6. UNIX for Dummies Questions & Answers

For some reason, my grep doesn't work as expected

I am trying to find only those entries where 7018 and another number appear in the end of the line. 7018 2828 1423 2351 7018 2828 14887 2828 7018 1222 123 7018 1487 I am looking for a way to generate only the last two lines. I was trying to do just "grep '7018{1,5}" but it does not... (5 Replies)
Discussion started by: Legend986
5 Replies

7. Shell Programming and Scripting

for loop doesn't work

I have a script which uses below for loop: for (( i = 0 ; i <= 5; i++ )) do echo "Welcome $i times" done But when I run the script, it gives error message: Syntex Error : Bad for loop variable Can anyone guide to run it? Thanks in advance. (10 Replies)
Discussion started by: naw_deepak
10 Replies

8. Solaris

grep -e doesn't work on solaris

grep -e doesn't work in Soalris. Same script with grep -e worked on AIX/HP/LINUX.. I would like to search a list of patterns on "log.txt" like ... grep -e FATAL -e ERROR log.txt I get the error message as grep: illegal option -- e Usage: grep -hblcnsviw pattern file . . . (3 Replies)
Discussion started by: jmkraja
3 Replies

9. UNIX for Dummies Questions & Answers

Why script For...Loop doesn't work. Seek help

I have written a script to run on UNIX server. When I tested, it always hanged on after "date +"%D %T: XXXXXX script started." part. Then it wouldn't go further. UNIX server gave me one error message. I used the same code in another script. It works fine. I think the major problem may be in... (3 Replies)
Discussion started by: duke0001
3 Replies

10. Shell Programming and Scripting

grep doesn't work within shell script?

I am trying to run the following code from a script file but it complains that syntax of (both instances of) grep is wrong. When I copy and paste it to the terminal, it is OK. Any idea what the problem might be? set i = `grep -c #define flags.h` while ($i>20) @ i-- my func (`cat... (4 Replies)
Discussion started by: barisgultekin
4 Replies
Login or Register to Ask a Question