Script to check for few conditions and respond with outputs
All,
Here is what I am trying to accomplish:
1. Check for a file of specific name recursively and when I find it,
a. if the file is more than 1hr old, no need to email or send notification, so just exit out from the shell script
b. if the file is less than 1hr old, send an email to me saying that there is a new file generated in this path and list out the path, timestamp etc
c. if the file is more than 24hrs old, delete the file and send an email that the file was deleted and then exit out of shell script.
And, here is what I have written until now.
Code:
#!/bin/ksh
>/tmp/prasad/ccorefilelist.txt
DIRECTORY=/tmp/prasad/
CCOREFILELIST=/tmp/prasad/ccorefilelist.txt
find ${DIRECTORY} -type f -name "ccore" -exec ls -leh {} \; >> $CCOREFILELIST
echo "find found files"
if [ -s "$CCOREFILELIST" ]
then
tmonth=`find ${DIRECTORY} -type f -name "ccore" -exec ls -leh {} \; | awk '{print $6}'`
echo "The Month of the core file is $tmonth"
tdate=`find ${DIRECTORY} -type f -name "ccore" -exec ls -leh {} \; | awk '{print $7}'`
echo "The Date of the core file is $tdate"
ttime=`find ${DIRECTORY} -type f -name "ccore" -exec ls -leh {} \; | awk '{print $8}'`
echo "The Time of the core file is $ttime"
thour=`find ${DIRECTORY} -type f -name "ccore" -exec ls -leh {} \; | awk '{print $8}' | cut -c1-2`
echo "The Hour of the core file is $thour"
cmonth=`date +%b`
echo "The current Month is $cmonth"
cdate=`date +%d`
echo "The current Date is $cdate"
ctime=`date +%T`
echo "The current Time is $ctime"
chour=`date +%H`
echo "The current Hour is $chour"
phour=`perl -e 'use POSIX qw(strftime); print strftime "%H", localtime(time()+$ARGV[0]);' -- -3600`
echo "The previous Hour is $phour"
yesterday=`perl -e '@T=localtime(time-86400);printf("%02d/%02d/%02d",$T[4]+1,$T[3],$T[5]+1900)'|cut -d '/' -f2`
echo "Yesterday is $yesterday"
if [[ "$tmonth" = "$cmonth" && "$tdate" = "$cdate" && "$thour" -lt "$phour" ]]
then
echo "New ccore files are generated today:"
echo "However this listing of ccore files is stale and no notification will be sent through email:"
elif [[ "$tmonth" = "$cmonth" && "$tdate" = "$cdate" && "thour" -lt "$chour" ]]
then
echo "entering the section of within last hour"
echo "The listing of ccore files generated within the last hour is as follows:" >> $CCOREFILELIST
mailx -s "$SUBJECT" test@test.com < $CCOREFILELIST
exit
elif [[ "$tdate" -lt "$yesterday" ]]
then
echo "The listing of ccore files generated is over a day old and hence will be deleted:"
####find logic to remove files older than 1 day######
fi
else
echo "There are no ccore files to be removed"
exit
fi
I think this script still has some logic missing like checking for other ccore file that may get generated within the hour etc. I am just using this as a template, and trying to refine and make it work.
Let me know if there is a simple and a better way to accomplish this task.
Last edited by pnara2; 09-24-2013 at 07:03 PM..
Reason: typo in script
Some hints (not exhaustive):
- if you got more than one ccore file in all subdirectories, your tmonth etc. variables will not be unique.
- why are you using four finds to extract those four vars? Use one and extract all variables from that.
- same for the date/time vars
- depending on your find version, you could use its time tests to analyse the one, more, and 24 hour conditions for the ccore files, list and - if need be - delete them, and send mails. No need for the cumbersome shell arithmetics!
Last edited by RudiC; 09-24-2013 at 07:55 PM..
Reason: typo
I wish to check two conditions inside the if statement
Condition 1: The two file contents should be identical // using cmp command for this.
Condition 2: The two filenames should NOT be the same.
This is what i did in vain.
if ]; then
where entry1 and entry2 are
ls *.txt | while... (7 Replies)
Hi, guys.
In Linux Shell script, how can I check a string whether meets some conditions.
e.g.:
If a string str must start with a underscore or a alphabet, and it must contains at least one lowercase, one uppercase, one numeric and one punctuation, and its length must be more than 8 characters... (2 Replies)
Hi,
Im programming an interactive menu that verifies the exports of my oracle DB, and im having some trouble finding a process that outputs for example a green command line when the export terminated successfully. i have something like this
cat... (15 Replies)
Hello, I want to write a script that takes a username as input and outputs the user's logins sorted by duration. Also I want to exclude the "still logged in" entries.
I use the "last" command but Im having problems sorting the entries based on the duration.
Can you help me?
Thanks a lot =) (4 Replies)
hi friends,
The code:
i=1
while
do
filename=`/usr/bin/ls -l| awk '{ print $9}'`
echo $filename>>summary.csv
#Gives the name of the file stored at column 9
count=`wc -l $filename | awk '{print $1}'`
echo $count>>summary.csv
#Gives just the count of lines of file "filename"
i=`expr... (1 Reply)
I have an expect script that I need to deal with a few different outputs? Here are the commands I am running
ssh -o StrictHostKeyChecking=no root@$ipaddress "racadm config -g cfgIpmiLan -o cfgIpmiLanEnable 1"
Now, the system I am running this from if I have connected to the server before I... (0 Replies)
Ok, I sort of need to create a command files that will be ftped to another server to run.
I have some input variable that will need to be read, and then transformed into another script file. Here are some examples.
Server 1:
outputCmd.sh
passing in ./outputCmd.sh nh8oaxt Release_4_0... (1 Reply)
function GetInput
{
print -n "Input"
read input
export INPUT=$input
}
export COMMAND="GetInput"
$COMMAND
echo "$INPUT"
$COMMAND | tee -a Log.log
echo "$INPUT"
The first one without "tee" works fine. echo "$INPUT" displays the values I type in for input. The second... (5 Replies)
First of all, im a total newbie to the point that i do not know what are the terms to search for my problem. I did however spend the rest of the day today trying to figure out what is wrong with my bash script. ive always thought that the best way to learn is to tackle a problem heads on. but at... (1 Reply)
Hi,
I have a bash script which I have referenced in the rc.local of my fedora linux OS. However it doesnt respond the same as when run in terminal from fedora.
The bash script has a series of interactive questions that require user input as shown:
#!/bin/bash
echo "Do you want to use... (1 Reply)