gpt4 book ai didi

linux - Bash 脚本 : perform actions based on file contents

转载 作者:太空宇宙 更新时间:2023-11-04 11:17:20 25 4
gpt4 key购买 nike

我有一个 bash 脚本,它循环遍历给定路径内的所有文件夹并执行一些操作。

我现在需要这个脚本通过监视我将从我的服务器更新的文件来自行找到给定的路径。

因此,脚本必须:

  1. 检查todo.txt文件是否存在
  2. 阅读文件的第一行
  3. 在第一行定义的文件夹内执行操作
  4. 删除todo.txt
  5. 将它在 todo.txt 中找到的同一行附加到 done.txt

出于某种原因,当我直接运行脚本时,效果很好

cd /proper/path/to/todo
FILE=todo.txt
if [ -f $FILE ];
then
FOLDER=$(head -1 $FILE)
echo "path to process:"$FOLDER
fi;

输出良好,文件被读取,输出为“path to process:/correct/path/read/from/file”

但是,当我设置一个 cron 作业来运行此脚本时,$FOLDER 变量为空,因此输出只是“处理路径:”

* * * * * /bin/sh /www/www.mysite.com/myscript.sh >> /www/www.mysite.com/log.txt

请注意:1.脚本运行2. if [ -f $FILE ] 有效,所以找到了文件

只是 head 命令失败了。

有什么想法吗?谢谢!

最佳答案

听起来 /usr/bin 不在 cron 的 PATH 中。试试这个:将此条目添加到 cron

* * * * * env > $HOME/cron.env

等一下,看看那个文件说了什么。


Don't parse ls -- 而不是 for i in $(ls) 执行 for file in *。由于 for 遍历 words,任何带有空格的文件名都不会被正确处理

关于linux - Bash 脚本 : perform actions based on file contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20273733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com