Find the answer to your Linux question:
Results 1 to 2 of 2
First of all i just want to say that I'm quite new to bash scripting. So, I'm trying to make a script to tell if it is day or night. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    9

    Day or night script?


    First of all i just want to say that I'm quite new to bash scripting.

    So, I'm trying to make a script to tell if it is day or night. The purpose of the script is to execute an action if it is day and a different one if it is night when the script is executed.

    So far i have this:

    Code:
    #!/bin/bash
    typeset -i TIME
    typeset -i DAY
    typeset -i NIGHT
    TIME=$(date +"%H")
    DAY='10'
    NIGHT='20'
    if [ ${TIME} -gt ${DAY} ]; then
    echo "It's day!"
    else
    exit
    fi
    if [ ${TIME} -gt ${NIGHT} ]; then
    echo "It's night!"
    else
    exit
    fi
    The problem here is that, when the clock is 21 (yes, I use a 24 hour clock), the script tells me it's both day and night. So how can i fix this to display only one of the options?

    The echoes are only there for testing purposes.

  2. #2
    Just Joined!
    Join Date
    Feb 2011
    Posts
    2
    Assuming that you want it to return "Day" between 10:00 and 20:59, and return "Night" between 21:00 and 09:59, you might want to try this:

    Code:
    #!/bin/bash
    TIME=$(date +"%H")
    DAY='10'
    NIGHT='20'
    if [ ${TIME} -ge ${DAY} -a ${TIME} -le ${NIGHT} ]; then
       echo "It's day!"
    else
       echo "It's night!"
    fi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •