Visit Our UNIX and Linux User Community


Multipe conditions in if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multipe conditions in if statement
# 1  
Old 08-28-2009
Multipe conditions in if statement

I have a script that runs every 15 minutes in cron that builds a web page.
It runs at 15, 28, 45 and 58 minutes past the hour. (pretty much evry 15 mins).

Every 2 hours from 6:28 to 18:28 it sends out emails if it finds errors. I do not want it sending email every single time it runs, every 15 mins.

So I have this statement:

if [ "$thour" = "06:28" ] || [ "$thour" = "08:28" ] || [ "$thour" = "10:28" ] || [ "$thour" = "12:28" ] || [ "$thour" = "14:28" ] || [ "$thour" = "16:28" ] || [ "$thour" = "18:28" ];then
send the mail .......

Is there a better way to do this?
something like

if $thour = "06:28" +2 hours up until "22:28" then ...... send the email

If I want to extend it beyond 18:28 as in my pretend statment above .. I'd like to be able to just change a range rather than keep adding more confitions in the if statement.

Whats more is that the script does a bunch of things and this timeline for sending emails is used a few times in the script like this:

build web page one
if errors are found send email to ABC with subject XYZ at times in this range

build web page two
if errors are found send email to DEF with subject UVW at times in this range

and so on .....
# 2  
Old 08-28-2009
then... why not using crontab?
# 3  
Old 08-28-2009
I am using crontab. The cron runs the script every 15 minutes.
I do not want the email every 15 minutes. I want the script to recognize that it's 6:28, time to send an email if there is one to send.

to use the crontab specifically for the email I'd have to have 2 versions of the script in crontab.
# 4  
Old 08-28-2009
Could use a case statement.

Code:
#!/bin/ksh
hour="`date '+%H:%M'`"
case "${hour}" in
    "06:28"|"08:28"|"10:28"|"12:28")
        echo "send_the_mail"
     ;;
esac

Personally I would have two cron lines and provide a parameter to the script for example "mail_yes" or "mail_no" .
# 5  
Old 08-28-2009
Code:
#!/usr/bin/bash

MINUTE=`date '+%M'`
HOUR=`date '+%H'`

case $MINUTE in
        28 )
                case $HOUR in
                     0[6-9] )
                         # insert send email statement(s) here
                          ;;
                     [1-2]* )
                         # insert send email statement(s) here
                          ;;
                esac
esac

# 6  
Old 08-28-2009
another way ...

by default, the email will be sent every 2 hours starting 0628 and ending 1828 unless the start and end times are changed ... need to set 3 arguments if needing to extend the end hour and 4 arguments if needing to change the minute with Y as the first argument ... $1 should be Y to send, N to not send, G to send immediately ... if G is selected, no other arguments are required ...


Code:
#! /bin/ksh
SEND=$1
SEND=${SEND:=Y}
START=$2
START=${START:=6}
END=$3
END=${END:=18}
MIN=$4
MIN=${END:=28}
MYHR=$(date +%H)
MYMIN=$(date +%M)

if [ $MYHR -ge $START -a $MYHR -le $END -a $MYMIN -eq $MIN ]
then
      ZERO=$(expr $MYHR % 2)
      if [ $ZERO = 0 ]
      then
            OK=Y
      else
            OK=N
      fi
fi

case $SEND in
Y) if [ $OK = "Y" ]
    then
          mail_report
    fi ;;
G) mail_report ;;
esac


this could be better but i don't have a sun box nor the time to test ... test properly before putting anything into production mode ...

Previous Thread | Next Thread
Test Your Knowledge in Computers #264
Difficulty: Easy
Alan Turing worked at MIT where he designed the Automatic Computing Engine, which was one of the first designs for a stored-program computer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Check Multiple conditions in IF statement?

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)
Discussion started by: mohtashims
7 Replies

2. UNIX for Dummies Questions & Answers

If statement with two boolean conditions

#!/bin/bash if && then echo "True" else echo "False" fi Hi everyone, I am new to UNIX, here I have a if statement elevating two boolean conditions. I thought the output should be True because there are + in the statement. But it turns out to be False. Can anyone... (3 Replies)
Discussion started by: mryuyu1111
3 Replies

3. Shell Programming and Scripting

Multiple conditions inside my if statement

Hello, I am using shell scripting and I am recieving odd results from my if statement if I want it to enter the loop only if L1 is equal to zero and one of the other criteria are filled, however it is entering at other times as well. What can i do to fix this? i tried seperating it... (6 Replies)
Discussion started by: ryddner
6 Replies

4. Shell Programming and Scripting

How to Evaluate two conditions in single if statement

I am trying to test two conditions in a single if and getting syntax error on -a and && if ] ; then echo "variable a equals to variable b" else echo "variable a not equal to variable b" fi in second attempt I used -a instead of &&, referring to other website, but not sure that... (1 Reply)
Discussion started by: praxis1
1 Replies

5. Shell Programming and Scripting

Multiple Conditions Perl if Statement

Hello, I'm trying to put together a script that involves pulling data from a config file. I'm attempting to write an if statement to validate one of the pieces of data from the config file, but I think I'm fat fingering it somehow. $config{VALUE} is being pulled from a config file but can only... (4 Replies)
Discussion started by: Picch
4 Replies

6. Shell Programming and Scripting

Multiple conditions in a CASE statement

is it possible to use multiple conditions in a CASE statement? And if so, what is the syntax? I'm trying to use one but can't seem to get it right. I want the statement to be CASE $vendor OR $alias condition 1) statements; condition 2) statements; etc. esac but I keep... (25 Replies)
Discussion started by: Straitsfan
25 Replies

7. Shell Programming and Scripting

If statement with multiple conditions

I have a script that runs on multiple servers. What I want to do is have the script do the following: if $(hostname) is equal to server or server2 then TO_DIR=go else TO_DIR=stop fi I have tried: if if ] Server is hpux. any ideas? (1 Reply)
Discussion started by: cpolikowsky
1 Replies

8. UNIX for Dummies Questions & Answers

Two conditions in one if statement

I'm totally new with bash programming and I don't get it how to put two conditions in one if statement. My code looks like this: h=`date +%k` if && ]; then$h is 10 but I don't get into my if statement. What's wrong here? (5 Replies)
Discussion started by: borobudur
5 Replies

9. Shell Programming and Scripting

How to giv two conditions in IF statement..??

Actually i need to satisfy both the condition.. lik i'm lookin for two different files.. and if BOTH the files r not present then it should run the followin script.. For example inputfile1=data/in/inputfile1.txt inputfile2=data/in/inputfile2.txt if then echo " " echo... (6 Replies)
Discussion started by: RRVARMA
6 Replies

10. Shell Programming and Scripting

if statement with two conditions

Hi, I am trying to use && set up to match two conditions within ksh: if && then '''Do something if somehow, I keep getting error message telling me that ] is missing. What's wrong with my code? Thanks a lot for your help! (1 Reply)
Discussion started by: cin2000
1 Replies

Featured Tech Videos