help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Before $time


From: Greg Wooledge
Subject: Re: Before $time
Date: Tue, 11 Jan 2022 18:33:58 -0500

> On Wed, Jan 12, 2022, 00:01 MN <promike1987@gmail.com> wrote:
> > 1) How should I compare the two time values? I think my approch is bad.

On Wed, Jan 12, 2022 at 12:24:24AM +0100, Alex fxmbsw7 Ratchev wrote:
> use date's %s seconds to compare

That's one way, yes (assuming GNU or BSD date).  You can also get both
times in HH:MM format, and use a string comparison to see which one is
later (greater).

> > 2) How good is the rest of the script?
> >
> > local d
> > d=$(date '+%H:%M')
> >
> > if [[ "$d" -lt "$1" ]]
> > then systemctl hibernate
> > else systemctl suspend
> > fi

You can only use "local" inside a function, and you haven't shown a
function definition.

Since you want to use a string comparison (your inputs have : characters
in them, and they're composed of base 10 values which may have leading
zeroes), you want < instead of -lt.  In the [[ command, -lt is an
integer comparison, and < is a string comparison.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]