bash - 用于比较日期的 Shell 脚本 If 语句
问题描述
我目前正在编写一个 shell 脚本来运行我的一些配置单元查询。在运行它之前,我需要检查表中的最后一个日期。我想检查日期并将其作为结果传递,然后将其与我的日期变量进行比较。我这样做是作为一个 if 语句,所以如果日期是昨天,那么它应该运行 hive 查询。如果不是(这意味着日期较旧且数据尚未刷新),我只希望它向我发送一封电子邮件告诉我。除了如何比较日期之外,我已经弄清楚了所有的逻辑。我尝试对 = 或 -gt、-lt 等进行重新保护的每一件事。我总是得到 THEN 语句,它不会检查第一个比较是否匹配。下面是我使用的代码。我有 then 声明,甚至告诉我它找到的日期。
这是我的查询:
#!/bin/bash
yesterday=`date -d "$date -3 days" +"%Y_%m_%d"`;
today=`date '+%Y_%m_%d'`;
cd /idn/home/chalse/AutoJobs
result=`hive -e "set hive.cli.print.header=false;select max(asgn_dt) from acorn_data.katie_rr;"`
if [ $result -eq $yesterday ]
then
echo "Hi Katie,
The last update for Reserve & Reactivation was $result .
Yesterday is $yesterday
Thanks,
CJ" | mailx -s "Reserve Reactivation Report $yesterday" - name@email.com
else
echo "Hi KAtie,
The last update for Commission File was $result. We even checked $today but we did not check $yesterday
Thanks,
CJ" | mailx -s "Reserve Reactivation Report $today" - name@email.com
fi
我收到的电子邮件显示 then 语句 The first date 表示 select hive 查询变量的输出。第二个只是今天的变量,第三个日期是 $yesterday 即使输出都显示答案是 2020-06-26,IF 语句不将它们识别为匹配。
"Hi KAtie,
The last update for Commission File was 2020-06-26($result). We even checked 2020_06_29($today) but we did not check 2020-06-26($yesterday)
Thanks,
CJ"
解决方案
看起来蜂巢是查询返回的不仅仅是日期。我假设它正在返回日志数据。
hive_result=`hive -S -e "set hive.cli.print.header=false; select max(asgn_dt) from acorn_data.katie_rr;"
一旦你有了hive_result
然后尝试根据分隔符进行拆分。
result=$(echo $hive_result| cut -d'_' -f 1)
接下来尝试比较两个结果,如下所示:
if [[ $result -eq $yesterday ]]
...
推荐阅读
- matlab - 在 Matlab 中实现抽象属性的问题
- google-api - 我正在尝试解决我的 api 无法正常工作的原因
- python - asyncio run_until_complete 在 future 设置结果后阻塞(高速公路 websockets 和线程)
- r - 循环更改为矩阵然后运行测试
- python - 如果用户输入为空,如何结束类中的方法
- html - 背景图像不适用于过渡
- sql-server - “导入平面文件”向导无法检测列标题
- c# - 使用对撞机 C# 触发角色动画
- python - 更改类中的变量会导致全局变量也发生更改?
- python - 为什么类中的python函数默认使用“self”作为传入的第一个参数