bash specific answer
As I like to reduce forks and bash do permit a lot of tricks, there is my purpose:
todate=2013-07-18
cond=2013-07-15
Well, now:
{ read todate; read cond ;} < <(date -f - +%s <<<"$todate"$'\n'"$cond")
This will re-populate both variables $todate
and $cond
, using only one fork, with ouptut of date -f -
wich take stdio for reading one date by line.
Finally, you could break your loop with
((todate>=cond))&&break
Or as a function:
myfunc() {
local todate cond
{ read todate
read cond
} < <(
date -f - +%s <<<"$1"$'\n'"$2"
)
((todate>=cond))&&return
printf "%(%a %d %b %Y)T older than %(%a %d %b %Y)T...\n" $todate $cond
}
Using bash's builtin printf
wich could render date time with seconds from epoch (see man bash
;-)
This script only use one fork.
Alternative with limited forks and date reader function
This will create a dedicated subprocess (only one fork):
mkfifo /tmp/fifo
exec 99> >(exec stdbuf -i 0 -o 0 date -f - +%s >/tmp/fifo 2>&1)
exec 98</tmp/fifo
rm /tmp/fifo
As input and output are open, fifo entry could be deleted.
The function:
myDate() {
local var="${@:$#}"
shift
echo >&99 "${@:1:$#-1}"
read -t .01 -u 98 $var
}
Nota In order to prevent useless forks like todate=$(myDate 2013-07-18)
, the variable is to be set by the function himself. And to permit free syntax (with or without quotes to datestring), the variable name must be the last argument.
Then date comparission:
myDate 2013-07-18 todate
myDate Mon Jul 15 2013 cond
(( todate >= cond )) && {
printf "To: %(%c)T > Cond: %(%c)T\n" $todate $cond
break
}
may render:
To: Thu Jul 18 00:00:00 2013 > Cond: Mon Jul 15 00:00:00 2013
bash: break: only meaningful in a `for', `while', or `until' loop
if outside of a loop.
Or use shell-connector bash function:
wget https://github.com/F-Hauri/Connector-bash/raw/master/shell_connector.bash
or
wget https://f-hauri.ch/vrac/shell_connector.sh
(Wich are not exactly same: .sh
do contain full test script if not sourced)
source shell_connector.sh
newConnector /bin/date '-f - +%s' @0 0
myDate 2013-07-18 todate
myDate "Mon Jul 15 2013" cond
(( todate >= cond )) && {
printf "To: %(%c)T > Cond: %(%c)T\n" $todate $cond
break
}