我正在研究Web应用程序的部署脚本。 该脚本的原作者写了以下内容,我不太明白。
find */target -name myapp*.jar -exec mv -t $CIRCLE_ARTIFACTS {} + || true谷歌搜索到目前为止,但我仍然有点不确定
{} + || true是用来。
I'm working on the deployment script for a web application. The original author of the script wrote the following which I don't quite understand.
find */target -name myapp*.jar -exec mv -t $CIRCLE_ARTIFACTS {} + || trueGoogling around has got me so far, but I'm still a little unsure with what
{} + || trueis used for.
最满意答案
{} +后缀是find的-exec动作的一部分:它会在每个mv调用中尽可能多地替换参数(也就是找到多个文件)。 (“尽可能多”是有限制的,因为操作系统用来存储环境变量和命令行参数的区域只有很多可用空间,所以如果find有很多结果,则可能需要多个mv调用-exec ... {} + )。
比较这个传统的命令:
find */target -name 'myapp*.jar' -exec mv -t "$CIRCLE_ARTIFACTS" {} ';'这会在找到每个文件时运行一次mv 。 由于/bin/mv是一个外部可执行文件,因此这是非常昂贵的。
相比之下,
find */target -name 'myapp*.jar' -exec mv -t "$CIRCLE_ARTIFACTS" {} +...运行尽可能少的mv调用。 (请注意,此用法需要-t扩展名,这不是由POSIX指定的;因此,它仅在GNU平台上可用)。
最后, || true || true只是一个布尔OR操作:如果find失败,则运行true ,强制命令始终导致真实的结果。 如果您使用set -e运行,这可以防止脚本在发生故障时退出。
这也可以更简洁地写为||: :,因为:是true同义词。
The {} + suffix is part of the -exec action of find: It substitutes as many arguments as possible (that is, as many files found) on each mv invocation. (There's a limit to "as many as possible" because there's only so much space available in the region the operating system uses to store environment variables and command-line arguments, so if find has many results, multiple mv invocations can be needed even with -exec ... {} +).
Compare to this traditional command:
find */target -name 'myapp*.jar' -exec mv -t "$CIRCLE_ARTIFACTS" {} ';'This runs mv once per file found. Since /bin/mv is an external executable, this is implicitly expensive.
By contrast,
find */target -name 'myapp*.jar' -exec mv -t "$CIRCLE_ARTIFACTS" {} +...runs as few mv invocations as possible. (Note that this usage requires the -t extension, which is not specified by POSIX; thus, it's only available on GNU platforms).
Finally, || true is simply a boolean OR operation: If the find fails, then true runs, forcing the command to always result in a truthful result. If you're running with set -e, this prevents the script from being exited in the event of a failure.
This can also be more tersely written as ||:, as : is a synonym for true.
更多推荐
发布评论