Bash 脚本中的时间变量 [英] Time Variables in Bash Scripting

查看:25
本文介绍了Bash 脚本中的时间变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果每次写入文件时我有 3 个不同的脚本要在不同的时间运行,那么如何编写 bash 脚本以仅在特定时间运行特定脚本.这不像 cron 作业那么简单(尽管 cron 可能会根据时间换出 .sh 文件),我正在寻找时间变量.

If I have 3 different scripts to run at various times each time a file is written to, how would a bash script be written to run a specific script only at a specific time. This is not as simple as a cron job (though cron could probably swap out the .sh file based on time), I am looking for the time variables.

例如:

If 9am-11:30 am run scriptA.sh if file.txt is changed. 
If 11:30am-5:45pm run scriptB.sh if file is changed.
If 5:46pm-8:59am run scriptC.sh if file is changed.

我问了一个类似的问题,但我认为我对我正在寻找的变量或如何定义它们不够清楚..

I asked a similar question but I don't think I was clear enough about the variables I am seeking or how to define them..

推荐答案

比较时间戳以确定是否需要执行工作的传统工具是 make.它的默认行为是计算指定目标的依赖链并确定是否有任何依赖文件发生了变化;如果不是,则不需要重新制作目标.这是避免重新编译的好工具,但它很容易扩展到其他任务.

The traditional tool for comparing time stamps to determine whether work needs to be performed or not is make. Its default behavior is to calculate a dependency chain for the specified target(s) and determine whether any of the dependent files have changed; if not, the target does not need to be remade. This is a great tool for avoiding recompilation, but it easily extends to other tasks.

具体而言,您将创建一个标志文件(例如,.made)并将其指定为依赖于您的 file.现在,如果 file 已更改,则需要重新创建 .made,因此 make 将运行您指定的命令来执行此操作.在这种情况下,我们将运行一段简单的 shell 脚本,然后 touch .made 将最新(成功)的运行时间传达给未来的运行.

In concrete terms, you'd create a flag file (say, .made) and specify it as dependent on your file. Now, if file has changed, .made needs to be recreated, and so make will run the commands you specify to do so. In this scenario, we would run a simple piece of shell script, then touch .made to communicate the latest (successful) run time to future runs.

剩下的是配方在不同时间运行不同的命令.我的方法是一个简单的 case 语句.请注意,make 解释美元符号,因此我们需要将那些应该传递给 shell 的美元符号加倍.

What remains is for the recipe to run different commands at different times. My approach to that would be a simple case statement. Notice that make interprets dollar signs, so we need to double those dollar signs which should be passed through to the shell.

.made: file
    case $$(date +%H:%M) in 
        09:* | 10:* | 11:[0-2]? ) 
            scriptA.sh ;; 
        11:[3-5]? | 1[2-6]:* | 17:[0-3]? | 17:4[0-5]) 
            scriptB.sh;; 
        17:4[6-9] | 17:5? | 1[89]:* | 2?:* | 0[0-8]:* ) 
            scriptC.sh;; 
    esac
    touch $@   # $@ expands to the current target

整个 case 语句需要作为单个逻辑行传递给 shell,因此我们最终会使用那些讨厌的反斜杠来转义换行符.

The entire case statement needs to be passed as a single logical line to the shell, so we end up with those pesky backslashes to escape the newlines.

还要注意 make 对缩进很挑剔;配方中的每个(逻辑)行都应以文字制表符开头.

Also notice that make is picky about indentation; each (logical) line in the recipe should be preceded by a literal tab character.

make 的默认行为是运行文件中的第一个目标;这个Makefile只包含一个target,所以make等价于make .made.

The default behavior of make is to run the first target in the file; this Makefile only contains one target, so make is equivalent to make .made.

还要注意 make 关心退出代码;如果 scriptAscriptBscriptC 可以以非零退出状态退出,则由 make 考虑作为致命错误,.made 文件将不会更新.(不过,您可以轻松防范这种情况.)

Also notice that make cares about exit codes; if scriptA, scriptB, or scriptC could exit with a non-zero exit status, that is regarded by make as a fatal error, and the .made file will not be updated. (You can easily guard against this, though.)

这篇关于Bash 脚本中的时间变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆