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!!!
issa7860Asked:
Who is Participating?
 
amit_gCommented:
Change

if [ $fileCount -gt 20 ]

to

if [[ $fileCount -gt 20 ]]
0
 
amit_gCommented:
Show us what you are doing now so that the minimum changes can be suggested.
0
 
issa7860Author Commented:
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.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
amit_gCommented:
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.$$
}
0
 
issa7860Author Commented:
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.$$
}
0
 
amit_gCommented:
Oh I missed a flag in wc. Change

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

to

fileCount=`wc -l < /tmp/DeleteFileTemp.$$`
0
 
issa7860Author Commented:
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.
0
 
issa7860Author Commented:
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.
0
 
amit_gCommented:
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?
0
 
issa7860Author Commented:
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
0
 
issa7860Author Commented:
Thanks the extra square parenthesis did the trick.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.