微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linux shell脚本出现循环错误

我正在尝试创build一个for循环来删除超过15天的日志文件。 以下是我的脚本:

#!/bin/sh path="/home/test" logpath="$path/logs" for logfile in `find $logpath -mtime +14 -type f -name *.log` do echo "Deleting Log File: " $logfile rm -rf $logfile done

它一直抛出一个错误

find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression]

有任何想法吗?

在expect脚本中包含#include

在C / C ++的Linux中获取磁盘标签

如何在Linux内核级别限制特权用户访问?

“服务postgresql开始”和“pg_ctl开始”有什么区别?

在OpenBSD到Linux中,Grep的行为是不一样的。 无法获得命令的工作

Eclipse:Ctrl + Shift +? Linux / KDE中的热键?

TSTP(礼貌暂停)如何与我的C ++程序在Linux中进行交互?

我在哪里可以find关于MAKEFILE的写作教程?

在Linux中的mmap持久性

:用不同的用户执行一个Python脚本会产生不同的行为

请尝试这个 – 添加单引号

#!/bin/sh path="/home/test" logpath="$path/logs" for logfile in `find $logpath -mtime +14 -type f -name '*.log'` do echo "Deleting Log File: " $logfile rm -rf $logfile done

您可以使用find的exec参数来摆脱for循环:

find $logpath -mtime +14 -type f -name '*.log' -exec rm -rf {} ;

或者像@Patryk Obara说的:

find $logpath -mtime +14 -type f -name '*.log' -delete

它隐式启用 – -depth 。

你可以像这样测试它:

mkdir test cd test touch test1.log touch test2.log find . -type f -name '*.log' ls > test1.log test2.log find . -type f -name '*.log' -delete > empty

对于这种工作,Logrotate是一个更好的选择,这是一个链接到它的文档 – http://www.linuxcommand.org/man_pages/logrotate8.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐