Search code examples
bashshelltimeshellcode

Shell Script with time windows and a break in between


I can't figure out how I could get that script to work the correct way.

The subscript will run if it is in the timeframe as example 0800 - 1700 and if it is NOT in the pause time 1200 - 1400.

Here is my shellscript.

if [[ "$socket8_timestart" -le "$timenow"  &&  "$timenow" -le "$socket8_timestop" ]] &&
[[ "$timenow" -lt "$socket8_breakstart"  &&  "$socket8_breakstop" -gt "$timenow" ]] ; then
subscript

Could you please help me?


Solution

  • "$timenow" -lt "$socket8_breakstart"  &&  "$socket8_breakstop" -gt "$timenow" 
    

    this condition give true only before break, not within break (as intended) but also not after break. which is probably the problem you see.

    consider chaniging the logic slightly:

    #!/bin/bash
    timenow="1330"
    socket8_timestart="900"
    socket8_breakstart="1300"
    socket8_breakstop="1400"
    socket8_timestop="1500"
    
    (( timenow > socket8_timestart &&
       timenow < socket8_timestop
    )) && ((
       timenow < socket8_breakstart ||
       timenow > socket8_breakstop
    )) && echo "running"
    

    alternatively, don't look at break as exception, but split the whole interval into two, one pre-break and one post-break. run script if pre-break OR post-break matches.

    Beware of leading zeroes in your times. Pause testing has been put into a seperate clause for easier factoring. There's no need for an extra clause, but may simplify things.