sed 和 regex 修改 crontab

sed and regex to modify crontab

当我尝试使用 sed 和正则表达式修改 crontab 条目时遇到问题。

crontab 的内容:

40 09 21 02 * cd /toto/tata && ./script.sh  1>../log/script_customer1.log 2>../log/script_customer1.err #customer1 #type1 #editable
00 19 21 05 * cd /toto/tata && ./script.sh  1>../log/script_customer2.log 2>../log/script_customer2.err #customer2 #type1 #editable

我的脚本:

cust_hrt="#customer1 #type1"
crontab -l | sed -e 's/\([0-9]*\) \([0-9]*\) \([0-9]*\) \([0-9]*\) \(.*${cust_hrt} #editable\)/30 22 25 05 /'

但它不起作用。 如果我直接尝试:

crontab -l | sed -e 's/\([0-9]*\) \([0-9]*\) \([0-9]*\) \([0-9]*\) \(.*#customer1 #type1 #editable\)/30 22 25 05 /'`

然后就可以了。

我不明白。 我是否遗漏了变量的某些内容?

单引号内的变量未被插值。如果您切换到对 sed 命令使用双引号,它应该可以工作。