使用sed从脚本更新crontab作业分钟和小时

时间:2021-02-25 01:09:06

I'm using a script (Javascript, Node) to update a single line of my crontab file, identified by a #comment at the end.

我正在使用脚本(Javascript,Node)更新我的crontab文件的一行,最后由#comment标识。

I think I don't fully understand how to regex within sed, more particularly how are match groups defined and used after it.

我想我并不完全理解如何在sed中使用regex,更具体地说,如何在其后定义和使用匹配组。

Say I want to update the minutes and hour of my cron job. I now use two different lines I figured out by pure trial and error after lots of searching:

说我想更新我的cron工作的分钟和小时。我现在使用了两条不同的线路,经过大量搜索后,我通过纯粹的试验和错误找到了这条线:

To update the minutes:

要更新分钟数:

"crontab -l | sed -e 's/[0-9]* \\([0-9]*\\) \\(.*#mycomment\\)/ mynewminutes \\1 \\2/' | crontab -"

And to update the hour:

并更新小时:

"crontab -l | sed -e 's/\\([0-9]*\\) [0-9]* \\(.*#mycomment\\)/\\1 mynewhour \\2/' | crontab -"

I would like to do this in a single line of code, but I can't get it to work even though I feel I'm so close. Any ideas? I got to a point where I only see a bunch of / and \ without meaning within the sed command, if somebody could explain that command and regex to me I'd be so thankful.

我想在一行代码中做到这一点,但即使我觉得我如此接近,我也无法让它工作。有任何想法吗?我得到了一个点,在sed命令中我只看到一堆/和\没有意义,如果有人可以向我解释这个命令和正则表达式,我会非常感激。

Example:

Existing cron job which will execute command /path at 11:00

现有的cron作业将在11:00执行命令/路径

00 11 * * * command /path #mycomment

If I execute the above commands with mynewminutes = 30 and mynewhour = 20 cron will execute the task at 20:30, with the resulting updated cron entry in the cron file:

如果我用mynewminutes = 30和mynewhour = 20执行上述命令,cron将在20:30执行任务,并在cron文件中生成更新的cron条目:

30 20 * * * command /path #mycomment

Please note both lines are quoted and escaped because they are Javascript strings which I'm calling using Node's exec.

请注意这两行是引用和转义的,因为它们是Javascript字符串,我正在使用Node的exec调用。

1 个解决方案

#1


0  

sed by default uses the BRE (Basic Regular Expression) syntax, that's why capture brackets are escaped with a backslash. But to figure a literal backslash in a Javascript string, you also need to escape it. That's why brackets are preceded by two backslashes.

默认情况下,sed使用BRE(基本正则表达式)语法,这就是使用反斜杠转义捕获括号的原因。但是要在Javascript字符串中计算字面反斜杠,您还需要将其转义。这就是为什么括号前面有两个反斜杠。

About how to change the hour and the minutes in a single replacement, it isn't very complicated:

关于如何在一次更换中更改小时和分钟,这不是很复杂:

sed -e 's/^[0-9][0-9]* [0-9][0-9]* \(.*#mycomment\)/mynewminutes mynewhour \1/'

pattern details:

^             # start of the line anchor
[0-9][0-9]*   # one or more digits
              # space
[0-9][0-9]*   #
              #
\(.*#mycomment\) # capture group 1: all until #mycomment

replacement: \1 refers to the content of capture group 1

替换:\ 1指捕获组1的内容

or better: (this way you avoid capture groups)

或更好:(这样可以避免捕获组)

sed -e '/#mycomment/s/^[0-9][0-9]* [0-9][0-9]*/mynewminutes mynewhour/'

(that replaces the two first numbers on the line only if the /#mycomment/ condition succeeds)

(仅当/ #mycomment / condition成功时才替换行上的两个第一个数字)

Note that using awk is an interesting alternative since it's designed to work with fields:

请注意,使用awk是一个有趣的选择,因为它设计用于字段:

awk '/#mycomment/{$1=mynewminutes;$2=mynewhour}1'

Once in your Javascript string you obtain:

进入Javascript字符串后,您将获得:

"crontab -l | sed -e 's/^[0-9][0-9]* [0-9][0-9]* \\(.*#mycomment\\)/mynewminutes mynewhour \\1/' | crontab -"

or

"crontab -l | sed -e '/#mycomment/s/^[0-9][0-9]* [0-9][0-9]*/mynewminutes mynewhour/' | crontab -"

or:

"crontab -l | awk '/#mycomment/{$1=mynewminutes;$2=mynewhour}1' | crontab -"

About the awk version, if you want a leading 0, you have to enclose the new hour or minutes between quotes:

关于awk版本,如果你想要一个前导0,你必须在引号之间包含新的小时或分钟:

"crontab -l | awk '/#mycomment/{$1=30;$2=\"05\"}1' | crontab -"

#1


0  

sed by default uses the BRE (Basic Regular Expression) syntax, that's why capture brackets are escaped with a backslash. But to figure a literal backslash in a Javascript string, you also need to escape it. That's why brackets are preceded by two backslashes.

默认情况下,sed使用BRE(基本正则表达式)语法,这就是使用反斜杠转义捕获括号的原因。但是要在Javascript字符串中计算字面反斜杠,您还需要将其转义。这就是为什么括号前面有两个反斜杠。

About how to change the hour and the minutes in a single replacement, it isn't very complicated:

关于如何在一次更换中更改小时和分钟,这不是很复杂:

sed -e 's/^[0-9][0-9]* [0-9][0-9]* \(.*#mycomment\)/mynewminutes mynewhour \1/'

pattern details:

^             # start of the line anchor
[0-9][0-9]*   # one or more digits
              # space
[0-9][0-9]*   #
              #
\(.*#mycomment\) # capture group 1: all until #mycomment

replacement: \1 refers to the content of capture group 1

替换:\ 1指捕获组1的内容

or better: (this way you avoid capture groups)

或更好:(这样可以避免捕获组)

sed -e '/#mycomment/s/^[0-9][0-9]* [0-9][0-9]*/mynewminutes mynewhour/'

(that replaces the two first numbers on the line only if the /#mycomment/ condition succeeds)

(仅当/ #mycomment / condition成功时才替换行上的两个第一个数字)

Note that using awk is an interesting alternative since it's designed to work with fields:

请注意,使用awk是一个有趣的选择,因为它设计用于字段:

awk '/#mycomment/{$1=mynewminutes;$2=mynewhour}1'

Once in your Javascript string you obtain:

进入Javascript字符串后,您将获得:

"crontab -l | sed -e 's/^[0-9][0-9]* [0-9][0-9]* \\(.*#mycomment\\)/mynewminutes mynewhour \\1/' | crontab -"

or

"crontab -l | sed -e '/#mycomment/s/^[0-9][0-9]* [0-9][0-9]*/mynewminutes mynewhour/' | crontab -"

or:

"crontab -l | awk '/#mycomment/{$1=mynewminutes;$2=mynewhour}1' | crontab -"

About the awk version, if you want a leading 0, you have to enclose the new hour or minutes between quotes:

关于awk版本,如果你想要一个前导0,你必须在引号之间包含新的小时或分钟:

"crontab -l | awk '/#mycomment/{$1=30;$2=\"05\"}1' | crontab -"