在 shell 脚本中执行类似 crontab 的数字匹配
模拟 crontab 行前五个字段中的表达式所使用的相同数字匹配的最直接方法是什么?
例如,给定输入模式“1,5-7,16,*/3”(愚蠢的例子,我知道)和值“6”,输出将是布尔值 true。
如果没有一个非常简单的解决方案,那么在我的情况下提供第三个输入是现实的,该输入将指定星号需要匹配的最大值,以便可以翻译星号(以及连字符的范围)到值列表,并且输入值可以与该列表进行匹配。 (上面示例模式的列表将为“1,3,5,6,7,9,12,15,16,18”,最大值为“18”。)
谢谢!
What would be the most straightforward way to emulate the same numeric matching that is used for the expressions in the first five fields of a crontab line?
For example, given inputs of a pattern "1,5-7,16,*/3" (silly example, I know) and a value "6", the output would be a boolean true.
If there isn't a dead simple solution, it'd be realistic in my situation to provide a third input which would specify the maximum value that an asterisk would need to match, so that asterisks (along with the hyphenated ranges) could be translated to a list of values and the input value could be matched against that list. (The list of the example pattern above would be "1,3,5,6,7,9,12,15,16,18", given a maximum value of "18".)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我主要是一个 ksh 人,但我对 bash 的经验表明这应该有效(给出你的例子),或者至少指出你需要做什么。
编辑 2018-08-20 对于
bash
,您需要将print --
更改为echo ....< /code> 或
printf "%s\n"
。实际上,我会删除
print -- ""
内容,然后调用exit 0
或exit 1
,然后退出适当的返回代码,然后可以由调用进程进行测试。为了包括你的例子的其余部分,我必须
这样做所以,这可能会令人兴奋!
(哦,你可能可以在 case 语句之前得到结果,
只需将值合并到....)将所有派生值保存为
变量,
(可能是 bash 中的 (( ${hrVal} / 3 ))
IHTH
I'm mostly a ksh person, but my experience with bash says this should work (given your example), or at least point you towards what needs to be done.
Edit 2018-08-20 For
bash
, you'll need to changeprint --
to eitherecho ....
orprintf "%s\n"
.In reality, I would remove the
print -- ""
stuff, and just callexit 0
orexit 1
, which will then exit with the appropriate return code, that can then be tested by the calling process.to include the rest of your example, I had to do
So, this could be exciting!
(oh, you can probably get the result before the case statement and
just merge the value into the ....) save all derived values as
variables,
(maybe it is (( ${hrVal} / 3 )) in bash )
IHTH