bash specific answer
bash specific answer
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
}