Get current time (hh:mm) in shell script and branch with if statement

Asked 2 years ago, Updated 2 years ago, 73 views

I'd like to write a program that takes the current time (hh:mm) in the shell script and branches conditions between 12:00 and 13:00 in the if statement.

In this case,

sec_time=date'+%h'
start_time = 12:00
finish_time = 13:00

if [$secion_time-gt$finish_time]; then
    if [$secion_time-lt$finish_time]; then
        echo processing start
    fi
fi

If this is the case, the format of the date type is incorrect and does not work, so please let me know if there is a correct format or better way to write it.

shellscript

2022-09-30 13:53

2 Answers

This response is based on comments from metropolis.

If it doesn't include 13:00

if[$(date'+%H')-eq12];then
    echo 'Start processing'
fi

I wonder if it's okay.


2022-09-30 13:53

You can compare it as a number if you do not include : in the time.

now=$(date'+%H%M')
start_time = 1200
finish_time = 1300

echo$now

if [$now-ge$start_time-a$now-lt$finish_time]; then
    echo processing start
fi


2022-09-30 13:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.