linux - 日期和条件 bash 命令的奇怪但非常简单的问题

标签 linux bash date conditional-statements

我创建了一个每小时创建增量备份的 bash 脚本,它是通过 crontab 调用的。这个脚本完美无缺,每次运行只需要几分钟。
问题是我在脚本的开头有一个条件,它允许我暂时禁用备份(凌晨 5 点除外),只需创建一个空文件“backup.disabled”即可。
除了这个备份脚本,没有其他东西可以在这里创建这些目录,当然备份脚本不会备份它自己的备份,我确认它们每次从一个 NVME 驱动器运行到另一个只需要大约 1-5 分钟。
条件似乎不能正常工作,尽管它非常基本。

# Do not run the backup if the file 'backup.disabled' exists and it is not currently 5 am.
if [[ -e backup.disabled && "`date +%H`" -ne "05" ]] ; then
    exit
fi

# Start the backup
BACKUP_DIR_NAME="`date +%F_%H%M`"
#.... everything after this works perfectly, no need to show it
以下是在 10 月 13 日 21:30 左右创建“backup.disabled”后生成的备份目录:
#...
drwxr-xr-x 1 root root  34 Oct 13 07:05 2020-10-13_0700
drwxr-xr-x 1 root root  34 Oct 13 08:03 2020-10-13_0800
drwxr-xr-x 1 root root  34 Oct 13 09:02 2020-10-13_0900
drwxr-xr-x 1 root root  34 Oct 13 10:02 2020-10-13_1000
drwxr-xr-x 1 root root  34 Oct 13 11:01 2020-10-13_1100
drwxr-xr-x 1 root root  34 Oct 13 12:01 2020-10-13_1200
drwxr-xr-x 1 root root  34 Oct 13 13:02 2020-10-13_1300
drwxr-xr-x 1 root root  34 Oct 13 14:04 2020-10-13_1400
drwxr-xr-x 1 root root  34 Oct 13 15:01 2020-10-13_1500
drwxr-xr-x 1 root root  34 Oct 13 16:01 2020-10-13_1600
drwxr-xr-x 1 root root  34 Oct 13 17:04 2020-10-13_1700
drwxr-xr-x 1 root root  34 Oct 13 18:06 2020-10-13_1800
drwxr-xr-x 1 root root  34 Oct 13 19:03 2020-10-13_1900
drwxr-xr-x 1 root root  34 Oct 13 20:01 2020-10-13_2000
drwxr-xr-x 1 root root  34 Oct 13 21:01 2020-10-13_2100
# at this point I created 'backup.disabled'
drwxr-xr-x 1 root root  34 Oct 14 05:05 2020-10-14_0500
drwxr-xr-x 1 root root  34 Oct 14 08:01 2020-10-14_0800
drwxr-xr-x 1 root root  34 Oct 14 09:02 2020-10-14_0900
drwxr-xr-x 1 root root  34 Oct 15 05:01 2020-10-15_0500
drwxr-xr-x 1 root root  34 Oct 15 08:00 2020-10-15_0800
drwxr-xr-x 1 root root  34 Oct 15 09:00 2020-10-15_0900
问题:事实证明,它会在早上 5 点、早上 8 点和早上 9 点创建备份......但它只应该在文件“backup.disabled”存在时在早上 5 点创建备份,除非我遗漏了什么? ??
早上8点和9点的备份似乎都是很正常的备份……
无论哪种方式,我都觉得这个问题很奇怪,并想了解发生了什么。
谢谢你。

最佳答案

将字符串(在本例中为数字)与数字测试操作数(例如, -eq )进行比较时会出现问题。
考虑以下:

$ for i in {01..12}
do
   echo "++++++++++++++ ${i}"

   if [[ "${i}" -ne "05" ]]
   then
       echo "not equal"
       continue
   fi

   echo "here i am"
done

++++++++++++++ 01
not equal
++++++++++++++ 02
not equal
++++++++++++++ 03
not equal
++++++++++++++ 04
not equal
++++++++++++++ 05
here i am
++++++++++++++ 06
not equal
++++++++++++++ 07
not equal
++++++++++++++ 08
./testdt: line 7: [[: 08: value too great for base (error token is "08")
here i am
++++++++++++++ 09
./testdt: line 7: [[: 09: value too great for base (error token is "09")
here i am
++++++++++++++ 10
not equal
++++++++++++++ 11
not equal
++++++++++++++ 12
not equal
请注意,对于 i = 08 / 09我们得到转换错误,因为 shell 试图处理字符串 '08''09'和 base-8(八进制)数字。 [请参阅这些涵盖同一问题的问答:herehere ]
另请注意,在出现错误后,脚本会继续并打印“我在这里”消息,这在您的脚本中等同于执行备份(@ 08:00 和 09:00)。
要解决此问题,您可以将值测试为数字(使用 -ne )或使用 !=如果您想继续比较字符串,例如:
$ for i in {01..12}
do
   echo "++++++++++++++ ${i}"

   if [[ "${i}" != "05" ]]                    # use '!=' to compare strings
   then
       echo "not equal"
       continue
   fi

   echo "here i am"
done

++++++++++++++ 01
not equal
++++++++++++++ 02
not equal
++++++++++++++ 03
not equal
++++++++++++++ 04
not equal
++++++++++++++ 05
here i am
++++++++++++++ 06
not equal
++++++++++++++ 07
not equal
++++++++++++++ 08               # no error; 'here i am' message not printed
not equal
++++++++++++++ 09               # no error; 'here i am' message not printed
not equal
++++++++++++++ 10
not equal
++++++++++++++ 11
not equal
++++++++++++++ 12
not equal

关于linux - 日期和条件 bash 命令的奇怪但非常简单的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64375614/

相关文章:

linux - 亚马逊 EC2 : Unable to unmount and remove EBS drive file system

c - 存储在.data 或.bss 段中的静态或全局数据是否应该在程序执行前加载?

javascript - 使用 Gmail 应用脚本按月对标签计数进行分组

linux - ctrl+c 不终止进程

linux - 如何使用 bash 从文本文件中删除前两行和后四行?

date - Doctrine 的日期字段——query 的写法

swift - MMM dd, yyyy hh :mm:ss a? 的正确日期格式是什么以及如何转换为 dd-mm-yyyy HH:ii

java - 停止Linux服务器中的未知进程

PHP - 在 cron 作业中从 Amazon S3 读取大型 XML 文件导致进程内存不足

linux - 从 bash 输出中排除一个字符串