Bash:当指定文件夹为空时,通过find命令退出脚本 [英] Bash: exit a script when a specified folder is empty via the find command
问题描述
我要检查文件夹是否为空.如果是,则编写一个日志文件并退出脚本.
I want to check whether a folder is empty. If yes, write a log file and exit the script.
我的代码如下:
find $folder -maxdepth 0 -empty -exec echo [`date`] $folder is empty > log.txt & exit 0 \;
但是,我收到了
find: missing argument to `-exec'
您能帮我解决问题吗?
谢谢!
===========================================================
===========================================================
谢谢大家!
我只是试图只退出没有输出日志的脚本:
I just tried only to exit the script without output log:
find $folder -maxdepth 0 -empth -exec exit 0 \;
或
find $folder -type d -empty -exec exit 0 \;
我收到错误:
find: ‘exit’: No such file or directory
即使我将 set -e
放在脚本顶部,该脚本仍能继续运行.我不知道如何解决它.
And the script keeps running even I have put set -e
at the top of the script.
I have no idea how to fix it.
推荐答案
如果我正确理解了这个问题,这应该可以工作:
If I understand the question correctly, this should work:
if find "$folder" -maxdepth 0 -empty | grep -q "."; then
echo "[$(date)] $folder is empty" > log.txt # Should this be >> to append?
exit 0
fi
说明:如果目录 $ folder
为空,则 find
命令将打印其路径; grep
检查是否有任何东西(".
"是匹配任何字符的模式),如果匹配则退出并显示成功状态;否则,返回成功.然后 if
语句根据 grep
的退出状态运行或跳过其内容.
Explanation: if the directory $folder
is empty, the find
command prints its path; grep
checks to see whether it got anything (".
" is a pattern that matches any character), and exits with a success status if there was a match; the if
statement then runs or skips its content based on grep
's exit status.
说明原始代码为何不起作用的原因: -exec
中的命令不是完整的shell命令,只是将要传递给 execl()
系统调用(或相关功能).也就是说,它是可执行文件的文件名(可以在常见的 PATH
中找到),后跟要传递给它的参数列表.
Explanation of why the original didn't work: the command in -exec
is not a full shell command, it's just a series of words that're going to be passed to the execl()
system call (or a related function). That is, it's a filename of an executable (to be found in the usual PATH
) followed by a list of arguments to be passed to it.
这有两个重要的含义:您不能在命令中使用任何任何 shell功能,因此不进行任何重定向(> log.txt
),不加入任何多个命令通过&
或;
或 |
或类似的方法,没有变量替换等.就您的命令而言,所有这些将通过外壳 before 进行解析,然后将结果传递给 find
-因此,重定向到log.txt的过程将发生在整个 find
命令中, date
替换发生在运行 find
之前,&
将find命令置于后台,并将之后的部分视为完全不同的命令.
This has two important implications: you can't use any shell features in the command, so no redirects (> log.txt
), no multiple commands joined by &
or ;
or |
or anything like that, no variable substitutions, etc. In the case of your command, all of those things will get parsed by the shell before passing the result to find
-- so the redirect to log.txt happens to the entire find
command, the date
substitution happens before find
is run, the &
puts the find command in the background and treats the part after as a completely different command.
此外, exit
不是可以与 execl()
或类似代码一起运行的常规命令;它是内置的shell(请尝试 type -a exit
),因此它不能由 -exec
执行.即使可以,它也不会做您想要的事情,因为由 -exec
运行的命令是作为子进程运行的,它只会退出该子进程,而不是整个脚本(甚至是退出) find
命令).
Furthermore, exit
is not a regular command that can be run with execl()
or such; it's a shell builtin (try type -a exit
), and so it cannot be executed by -exec
. And even if it could, it wouldn't do what you want, because the command run by -exec
is run as a subprocess, and it would just exit that subprocess, not the entire script (or even the find
command).
这篇关于Bash:当指定文件夹为空时,通过find命令退出脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!