这不起作用:
find "$all_locks" -mindepth 1 -maxdepth 1 -type d | basename
显然 basename 无法从 stdin 读取 - 在任何情况下 basename 都需要至少一个参数。
最佳答案
要将命令应用于管道操作的每个结果,xargs是你的 friend 。正如我链接的手册页上所说的那样......
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input.
在这种情况下,这意味着它将从您的
find
中获取每个结果。命令并运行 basename <find result>
ad nauseum,直到 find 完成其搜索。我相信你想要的看起来很像这样:find "$all_locks" -mindepth 1 -maxdepth 1 -type d | xargs basename
关于bash - 如何通过管道查找路径的基本名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56511834/