bash – Unix find -exec:为什么以下行为有所不同?
发布时间:2020-12-16 01:49:34 所属栏目:安全 来源:网络整理
导读:以下按预期工作: $find . -name .git -exec dirname '{}' ;./google/guava./JetBrains/intellij-community./zsh-users/zsh-syntax-highlighting 但以下只返回点: $find . -name .git -exec echo "$(dirname '{}')" ;... 为什么这样,我如何在find -exec命
以下按预期工作:
$find . -name .git -exec dirname '{}' ; ./google/guava ./JetBrains/intellij-community ./zsh-users/zsh-syntax-highlighting 但以下只返回点: $find . -name .git -exec echo "$(dirname '{}')" ; . . . 为什么这样,我如何在find -exec命令中使用$(dirname'{}’)? 请注意,我问的是UNIX查找(OS X和FreeBSD,特别是),而不是GNU. 解决方法
行为差异的原因
您的shell在查找之前正在评估$(dirname),导致执行此命令: find . -name .git -exec echo . ; 其他方法 >你当然可以在find中使用shell扩展,通过自己调用另一个shell(或者更好,使用你想要的shell作为shebang调用脚本). find . -name .git -exec sh -c 'dirname {}' ; >没有dirname的解决方案(POSIX,更快,少一个子进程来调用): find . -name .git -exec sh -c 'path={}; echo "${path%/*}" ' ; >使用查找优化来梳理/u/tripleee’s answer(不是我) find . -name .git -exec sh -c 'for f; do echo "${f%/*}"; done' _ {} + (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |