获取重复输出

Getting duplicate output

我正在尝试编写一个 bash 脚本,该脚本在一个目录中查找文件并将其替换为源目录中的同名文件之一。当我 运行 find 命令时,它似乎设置了我的 $path 两次

 for f in build-res//*.png; do
     file="$(basename "$f")"
     echo "Looking for $file in $TMP"
     path="$(find $TMP -type f -name "$file")"
     if [[ -z $path ]]; then
             echo "Could not find $file in $TMP"
      else
             echo "Replacing file at $path with $file"
             echo "__path__"
             echo $path
             echo "---"
      fi
 done

运行 此循环的一次迭代输出类似于

Replacing file at tmp/trx//images/background/background_iphone5.png
tmp/trx//images/background_iphone5.png with background_iphone5.png
__path__
tmp/trx//images/background/background_iphone5.png tmp/trx//images/background_iphone5.png
---

注意路径如何在 space 之间重复。为什么会这样?

另一个注意,为什么它返回时路径中也带有 //?这似乎不是问题,更多的只是好奇。

如果你仔细观察,这不是同一条路径:

tmp/trx//images/background/background_iphone5.png tmp/trx//images/background_iphone5.png

-->

tmp/trx//images/background/background_iphone5.png 
tmp/trx//images/background_iphone5.png

这是find的结果输出,它在/tmp的不同子目录中找到2个同名文件。

仅供参考,如果您想控制 find 可以下降到子目录的深度,有一个选项:

-maxdepth levels

Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments.

或者如果您只想要一个结果,您可以使用

-quit

Exit immediately. No child processes will be left running, but no more paths specified on the command line will be processed. For example, find /tmp/foo /tmp/bar -print -quit will print only /tmp/foo.