bash script question - Linux

This is a discussion on bash script question - Linux ; 2 Questions : 1. I have the following BASH script #!/bin/bash hour=`date +%k` printf "hour = %d" $hour if [ $hour -lt 7 -a $hour -gt 18 ] ; then printf "in Time \n\n" else printf "\n\nOutta Time\n\n" fi hour=8 ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: bash script question

  1. bash script question

    2 Questions :

    1. I have the following BASH script

    #!/bin/bash


    hour=`date +%k`
    printf "hour = %d" $hour
    if [ $hour -lt 7 -a $hour -gt 18 ] ; then
    printf "in Time \n\n"
    else
    printf "\n\nOutta Time\n\n"
    fi


    hour=8
    printf "hour = %d" $hour
    if [ $hour -lt 7 -a $hour -gt 18 ] ; then
    printf "in Time \n\n"
    else
    printf "\n\nOutta Time\n\n"
    fi

    When I run it, the $hour variable is as axpected (21 & 8)
    however I always get "Outta Time"

    How do I do the comparison correctly ?

    2. What is the correct way from within the script to get the elapsed time...

    for example

    startTime=...?????

    long build process

    elapsed time= ???- $startTime

    print elapsed time (Hr : Min : Sec)

    TIA !
    Joe

  2. Re: bash script question

    On Tue, 17 Apr 2007 21:41:19 -0400, dada wrote:
    > 2 Questions :
    >
    > 1. I have the following BASH script
    >
    > When I run it, the $hour variable is as axpected (21 & 8)
    > however I always get "Outta Time"


    For debugging, you might try adding
    set -x # enable
    accross code to debug
    set - # disable

    > 2. What is the correct way from within the script to get the elapsed time...


    I would save "seconds since 1970-01-01 00:00:00 UTC"
    and use date with "now - $start_sec seconds" using the --date switch for
    math work. You already know the format commands.

    For extra points,
    man test
    man date
    http://tldp.org/LDP/abs/html/index.html

  3. Re: bash script question

    dada wrote:
    > ...
    > When I run it, the $hour variable is as axpected (21 & 8)
    > however I always get "Outta Time"
    >
    > How do I do the comparison correctly ?


    I think you want it this way:

    if [ $hour -gt 7 -a $hour -lt 18 ] ; then
    ^^^ ^^^

    ///gordian
    --

  4. Re: bash script question

    Gordian Jodlauk wrote:
    > dada wrote:
    > > ...
    >> When I run it, the $hour variable is as axpected (21 & 8)
    >> however I always get "Outta Time"
    >>
    >> How do I do the comparison correctly ?

    >
    > I think you want it this way:
    >
    > if [ $hour -gt 7 -a $hour -lt 18 ] ; then
    > ^^^ ^^^
    >
    > ///gordian


    yes a DUH moment,i'm afraid !

    Thanks for the help everyone.

    Joe

  5. Re: bash script question

    On Tue, 17 Apr 2007 21:41:19 -0400, dada wrote this:

    > 2 Questions :
    >
    > 1. I have the following BASH script
    >
    > #!/bin/bash
    >
    >
    > hour=`date +%k`
    > printf "hour = %d" $hour
    > if [ $hour -lt 7 -a $hour -gt 18 ] ; then
    > printf "in Time \n\n"
    > else
    > printf "\n\nOutta Time\n\n"
    > fi
    >
    >
    > hour=8
    > printf "hour = %d" $hour
    > if [ $hour -lt 7 -a $hour -gt 18 ] ; then
    > printf "in Time \n\n"
    > else
    > printf "\n\nOutta Time\n\n"
    > fi
    >
    > When I run it, the $hour variable is as axpected (21 & 8) however I always
    > get "Outta Time"
    >
    > How do I do the comparison correctly ?
    >
    > 2. What is the correct way from within the script to get the elapsed
    > time...
    >
    > for example
    >
    > startTime=...?????
    >
    > long build process
    >
    > elapsed time= ???- $startTime
    >
    > print elapsed time (Hr : Min : Sec)
    >
    > TIA !
    > Joe


    Do I understand the test correctly. ie, $hour lt 7 and $hour gt 18 ?

    Should it be $hour gt 18 or $hour lt 7 ~ hours from 7pm to 6am?

    Or $hour gt 7 and $hour lt 18 ~ hours from 8am to 6pm?


  6. Re: bash script question

    noi wrote:
    > On Tue, 17 Apr 2007 21:41:19 -0400, dada wrote this:
    >


    >> hour=`date +%k`
    >> printf "hour = %d" $hour
    >> if [ $hour -lt 7 -a $hour -gt 18 ] ; then
    >> printf "in Time \n\n"
    >> else
    >> printf "\n\nOutta Time\n\n"
    >> fi
    >>
    >>

    >
    > Do I understand the test correctly. ie, $hour lt 7 and $hour gt 18 ?
    >
    > Should it be $hour gt 18 or $hour lt 7 ~ hours from 7pm to 6am?
    >
    > Or $hour gt 7 and $hour lt 18 ~ hours from 8am to 6pm?
    >


    Like i said a DUH ! moment.

    was trying to test for a range in from 7 am to 6 PM

    so that a long running process would not run if the build ran during the
    day, but when it runs at night it does the extra processing.

    I ended up with

    if [ $hour -lt 7 -o $hour -gt 18 ] ; then
    do long running process
    else
    don't
    fi

    thanks !
    Joe

  7. Re: bash script question

    On Tue, 17 Apr 2007 21:41:19 -0400, dada wrote:

    > 2. What is the correct way from within the script to get the elapsed
    > time...
    > for example
    > startTime=...?????
    > long build process
    > elapsed time= ???- $startTime
    > print elapsed time (Hr : Min : Sec)


    Do a "man date" and look for the "%s" (seconds since the Epoch) flag.

    startTime=`date +%s`
    ....
    stopTime=`date +%s`
    elapsed=`expr $stopTime - $startTime`

    HTH

  8. Re: bash script question

    dada wrote:
    > 2. What is the correct way from within the script to get the elapsed
    > time...



    You could do this with the time command.

    for example, instead of "make" use "time make".

+ Reply to Thread