cronjob 中的 sed 不解释脚本变量

问题描述 投票:0回答:0

Debian 11 Bullseye 中的 Bash 脚本使用“sed”从命令行运行正常,但不能作为 cron

嗨,我正在跑步

Debian GNU/Linux 11 (bullseye) Linux 5.15.84-v8+ #1613 SMP PREEMPT Thu Jan 5 12:03:08 GMT 2023 aarch64 GNU/Linux

在我的设置中,我有两个 bash 脚本:

不同的脚本每天 24 小时不间断地每分钟用 JPEG 文件填充我的文件夹。这是一个 cronjob 并且运行完美。

当从 /home/myuser 中的命令行运行时,下面的脚本完美运行并创建一个文件,列出开始时间和结束时间之间 myfolder 中的所有 JPEG 文件

例如文件名

2023-03-05_1014.jpg

例如从命令行创建的 output.txt 我的文件夹/2023-03-05_1014.jpg

#!/bin/bash
cd /home/myuser/
current_date=$(date +%F)
time_start=2023-03-05_0900
time_finish=2023-03-05_1700

ls myfolder/"$current_date"_*.jpg | sed -n "/myfolder\/$time_start/, /myfolder\/$time_finish/p" > output.txt

当以用户 myuser(不是 root)作为 cronjob 运行时

00 22 * * * /home/myuser/create_jpg_list.sh 2>&1

output.txt 将列出 myfolder 中的每个文件以及从 0000 到 2359 列出的每个 JPEG 文件。也就是说,sed 命令中的变量似乎没有被解释。

例如从 cronjob 创建的 output.txt

myfolder/2023-03-05_2302.jpg
此行不应显示在 output.txt 中,因为它超出了上午 9 点到下午 5 点的时间范围

我在网上搜索过,有一些线程,例如下面的链接(仅举几例)处理这种情况,但是唉 https://unix.stackexchange.com/questions/614759/sed-doesnt-work-under-cron https://askubuntu.com/questions/524726/not-able-to-run-sed-on-bash-script https://unix.stackexchange.com/questions/662851/why-this-script-doesnt-work-from-cron-while-it-works-when-invoke-from-my-shell https://www.linuxquestions.org/questions/linux-newbie-8/sed-not-working-from-cron-706865/

欢迎任何反馈,因为我不知道还能尝试什么。

bash shell sed debian bullseye
© www.soinside.com 2019 - 2024. All rights reserved.