在bash脚本编程时​​间变量 [英] Time Variables in Bash Scripting

查看:90
本文介绍了在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..

推荐答案

比较时间戳来确定是否需要工作要执行与否是制作传统的工具。它的默认行为是计算指定目标(S)的依赖关系链,并确定是否有任何相关文件已更改;如果不是,则目标并不需要重拍。这是为了避免重新编译一个伟大的工具,但它很容易地扩展到其他任务。

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 ),并将其指定为依赖于你的文件。现在,如果文件已经改变, .made 需要重新创建,因此制作将运行指定这样做的命令。在这种情况下,我们会运行一段简单的shell脚本,那么触摸.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.

剩下的就是在配方,以在不同时间运行不同的命令。我对这种做法将是一个简单的情况语句。请注意,制作间$ P $点美元符号,因此,我们需要加倍的美元符号,应通过对外壳进行传递。

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

整个情况语句需要作为单个逻辑线壳传递,所以我们最终得到那些讨厌的反斜杠逃脱换行。

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.

另请注意,制作是挑剔缩进;在配方中每一个(逻辑)线应该是文字制表符pceded $ P $。

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

的默认行为制作是运行文件中的第一个目标;这个的Makefile 只包含一个目标,所以制作等同于使.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.

另请注意,制作在乎退出codeS;如果 scriptA scriptB scriptC 可以用一个退出非零退出状态,由算是让作为一个致命错误,而 .made 文件将不更新。 (您可以轻松地 href=\"http://stackoverflow.com/questions/2670130/make-how-to-continue-after-a-command-fails\">后卫,虽然。)

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天全站免登陆