Link to home
Start Free TrialLog in
Avatar of issa7860
issa7860

asked on

Unix shell script question to add email if deleting more than 20 directories

Hi,

I have a shell script that deletes core dump directories under OH/bdump directory but I want to add 1 more thing to the script and that is to send and an email when it deletes more than 20 directories.
Can anyone please help me ASAP.  I am using the find command to delete the directories.

Thanks in advance!!!
Avatar of amit_g
amit_g
Flag of United States of America image

Show us what you are doing now so that the minimum changes can be suggested.
Avatar of issa7860
issa7860

ASKER

The following is the script that deletes the directories:

delete()
{
  export ORAENV_ASK=NO
  export ORACLE_SID=$INSTANCE_NAME

  . oraenv

find $ORACLE_BASE/admin/$ORACLE_SID/bdump -name "cdmp_*"  -mtime +1 -exec rm -rf {} \;
}

and I want to add email if the directories deleted are more than 20.  The email has be before the deletion.
delete()
{
  export ORAENV_ASK=NO
  export ORACLE_SID=$INSTANCE_NAME

  . oraenv

  find $ORACLE_BASE/admin/$ORACLE_SID/bdump -name "cdmp_*"  -mtime +1 -exec echo "rm -rf " {} \; > /tmp/DeleteFileTemp.$$
  fileCount=`wc < /tmp/DeleteFileTemp.$$`

  if [ $fileCount -gt 20 ]
  then
     cat /tmp/DeleteFileTemp.$$ | mail -s "More then 20 files being deleted"  WhateverUser@WhateverDomain.com
  fi

  cat /tmp/DeleteFileTemp.$$ | sh
  rm /tmp/DeleteFileTemp.$$
}
Thanks. I used your script and it's deleting the files but getting the following error on unix command line after executing the script also not getting any emails:

purge[11]: 123: unknown test operator

and the script is:

delete()
{
  export ORAENV_ASK=NO
  export ORACLE_SID=$INSTANCE_NAME

  . oraenv

find $ORACLE_BASE/admin/$ORACLE_SID/issa -name "cdmp_*"  -mtime +1 -exec echo  "rm -rf" {} \; > /tmp/DeleteFileTemp.$$

  fileCount=`wc < /tmp/DeleteFileTemp.$$`

  if [ $fileCount -gt 20 ]
  then
     cat /tmp/DeleteFileTemp.$$ | mailx -s "${ORACLE_SID} on ${SERVER} More then 20 directories being deleted." username@domain.com
  fi

  cat /tmp/DeleteFileTemp.$$ | sh
  rm /tmp/DeleteFileTemp.$$
}
Oh I missed a flag in wc. Change

fileCount=`wc < /tmp/DeleteFileTemp.$$`

to

fileCount=`wc -l < /tmp/DeleteFileTemp.$$`
I did the change and now I am getting the following error:
purge[10]: 41:  not found

and no email yet?  the "cdmp_*" are directories.  Please let me know.

Thanks.
Looks like the count was 41 but then why it did not execute the next line and send the email.  The lines in the file is 41.  Please let me know ASAP.  Thanks.
What shell are you using? Add

set -x

fileCount=`wc -l < /tmp/DeleteFileTemp.$$`

and run it again. What messages do you see on the screen?
I am using KSH and got the following after running the script:

+ + wc -l
+ 0< /tmp/DeleteFileTemp.945
fileCount=      41
+ 41 -gt 20
/dba/bin/purge[11]: 41:  not found
+ sh
+ cat /tmp/DeleteFileTemp.945
+ rm /tmp/DeleteFileTemp.945
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks the extra square parenthesis did the trick.