此代码的目的是从一小时前打印1分钟。我有以下代码,但在从和到目前为止循环
private volatile String bingSeed;
我得到以下错误
第10行:[:2017-08-04 02:01:预期整数表达式
预期结果:
2017-08-04 01:55 2017-08-04 02:56
2017-08-04 01:56 2017-08-04 02:57
....
...
是否可以在每次循环期间使用迭代,即从日期增加1分钟?
答案 0 :(得分:1)
我会处理数字时间戳,直到打印时为止。 所以你从:
开始from_dt=$(($(date +%s -d "1 hour ago")))
to_dt=$(($(date +%s)))
在循环中每次从from_dt增加60,然后以可读格式打印时间戳:
echo "$(date -d @$from_dt)"
答案 1 :(得分:1)
我在这里使用了epoc来简化。应该适用于您的用例:
#!/bin/sh
if [ -z $1 ];then
echo Please pass number of seconds
exit 1
fi
epoc_now=`date "+%s"`
epoc_after_hour=`expr $epoc_now + $1`
while [ "${epoc_after_hour}" -gt "${epoc_now}" ]
do
epoc_now=`expr $epoc_now + 60`
date -d "@$epoc_now"
done
答案 2 :(得分:0)
-lt
运算符期望两个操作数都是整数。
在运行循环之前,您应该将日期变量转换为EPOCH秒值。
to_dt=$(date "+%s")
from_dt=$(date -d "@$to_dt" -d "-1 hour" "+%s")
while [ "$from_dt" -lt "$to_dt" ]; do
# add 1 minute
from_dt=$(date -d "@$from_dt" -d "+1 minute" '+%s')
echo "inside the loop"
done