Exit if between 2 times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit if between 2 times
# 1  
Old 09-21-2009
Exit if between 2 times

Hello,

I am working in Solaris 10 with a Bash script and trying to figure out how I can tell the script to look at the day of the week and the time and for example if it is Sat between 5:50am and 6:30am then just exit out of the script. Any other day or time keep running.

We have a switchover that takes place for some databases and I need to have a script that runs every minute just exit out during this time.

Any help would be great.

This is what I have so far... It works but no way to tell if its between 2 times.
Code:
#!/bin/sh
#
day=`date`
time=`echo $day | cut -d" " -f4`
hour=`echo $time | cut -d":" -f1`
minute=`echo $time | cut -d":" -f2`
dayname=`echo $day | cut -d" " -f1`

if [ $dayname = 'Mon' -a $hour = 12 -a $minute -lt 52 ]
then
echo "Before 12:52"
echo $dayname
echo $hour
echo $minute
exit

else

echo "Past 12:52"
echo $dayname
echo $hour
echo $minute
exit

fi

Thanks!

---------- Post updated at 09:25 AM ---------- Previous update was at 08:57 AM ----------

Figured it out :-)

Code:
#!/bin/sh
#
day=`date`
time=`echo $day | cut -d" " -f4`
hour=`echo $time | cut -d":" -f1`
minute=`echo $time | cut -d":" -f2`
dayname=`echo $day | cut -d" " -f1`
if [ $dayname = 'Mon' -a $hour = 13 -a $minute -lt 21 ]||[ $dayname = 'Mon' -a $hour = 13 -a $minute -gt 23 ]
then
echo "SAFE TO RUN SCRIPT"
echo $dayname
echo $hour
echo $minute
exit
else
echo "SCRIPT SHOULD DIE"
echo $dayname
echo $hour
echo $minute
exit
fi

Thanks!

Last edited by LRoberts; 09-21-2009 at 02:07 PM..
# 2  
Old 09-21-2009
you can format with the date command, it might help cleanup some of those variables.

date +%Y/%m/%d etc...
# 3  
Old 09-21-2009
Use simple arithmetic operations -
Code:
date "+1%w%H%M"

produces a 1 , the day of the week [ 0 - 6], 00-23 hours 00-59 minutes:
160550 is 550 am on Saturday, 160630 is 6:30 am on Saturday.

Code:
ok=0
now=$(date "+1%w%H%M")
if [[ $now -gt 160630 ||  $now -lt 160550 ]] ; then
    ok=1
else 
   exit
fi

or something like that...
# 4  
Old 09-21-2009
Let's remember what Useless Use of Cat Award stand for Smilie
Code:
#!/bin/sh
set -- `date "+%u %H %M"`
if [ $1 -eq 1  -a $2 -eq 17 ] && [ $3 -lt 21  -o $3 -gt 23 ]
then
        echo "SAFE TO RUN SCRIPT";
else
        echo "SCRIPT SHOULD DIE";
        exit 0;
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Response Times

Hello all. Let me qualify my question by saying that I am struggling with how to ask the question I am semi green but have no issue reading up if pointed in the right direction. Please be gentle! A RHEL server 6.2. Hosts a statistical application that has some web apps and batch programming... (0 Replies)
Discussion started by: rsheikh01
0 Replies

2. Shell Programming and Scripting

Need the difference between exit 1 & exit 7

Hi In one of the script I am seeing some thing like exit 7,exit 1,exit 2,exit 3,exit 9,exit6.What is the difference between all of this exit.Can anyone help here please (3 Replies)
Discussion started by: ginrkf
3 Replies

3. Shell Programming and Scripting

To get difference between 2 times.

Can anyone help me to write a script to get diffrence between 2 times. (platform- solaris) i.e., Diffence shoud be displayed in tems of hours. (Approximately) for eg: file1 = 05-Apr-2012 13:42:32 file2 = 04-Apr-2012 12:42:41 O/P is like : diff = 25 Hrs (2 Replies)
Discussion started by: thomasraj87
2 Replies

4. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

5. Shell Programming and Scripting

Executes many times

The code prints many times, "1st loop" and "2nd loop". How can I come out the if the condition is matched. Match should be only once and it should come out of the loop if the match is found time=$(date +"%H:%M:%S") echo $time while : do if then echo "1st loop" fi if then... (5 Replies)
Discussion started by: sandy1028
5 Replies

6. Shell Programming and Scripting

Getting logs between two times

Hi, I need to read log file for the period between two time frame.eg all those logs between 3AM to 4M. Any command to use for the shell scripting? Ahamed (5 Replies)
Discussion started by: ahamed
5 Replies

7. AIX

how would you know your server was rebooted 3 times or 5 times

Is there such location or command to know how many times did you reboot your server in that particular day?in AIX. (3 Replies)
Discussion started by: kenshinhimura
3 Replies

8. UNIX for Dummies Questions & Answers

what is meaning of exit(0) and exit(1)

can u tell me what is the meaning of exit(0),exit(1),exit(2) what is diff amonng these. Amit (1 Reply)
Discussion started by: amitpansuria
1 Replies

9. Programming

exit(0) versus exit(1)

What is the difference between using exit(0) and exit(1) to exit a program? Which should I use? (9 Replies)
Discussion started by: enuenu
9 Replies

10. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies
Login or Register to Ask a Question