有人可以解释一下这个 bash 末尾的符号的内部工作:“_ {} ;" [英] Can someone explain the inner working of the symbols at the end of this bash: “_ {} ;”

查看:13
本文介绍了有人可以解释一下这个 bash 末尾的符号的内部工作:“_ {} ;"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在shell中运行以下命令将.HEIC文件批量转换为.JPG文件,命令成功,但是有一部分我没有理解:

I ran the following command in shell to batch convert .HEIC files to .JPG files, the command is successful, however there's a part of it I don't understand:

find . -name '*.HEIC' -exec sh -c 'magick convert $1 "${1%.HEIC}.JPG"' _ {} ;

显然 _ {} 的作用是将 find 结果分配给 $1,但如何操作呢?我在谷歌和这里都找不到解释,而且 man find 也没有任何运气.答案完全有可能在这里,但这些符号不太好搜索.

Apparently _ {} acts to assign find result to $1, but how? I can't find an explanation on google nor here, and didn't have any luck with man find. It's entirely possible that answers were here but these symbols are not very nice to search for.

那么问题来了,_ {} 是如何给$1 赋值的呢?是否可以使用 find/或其他命令为其分配多个变量?

So the question is, how does _ {} assign variable to $1? Is it possible to assign multiple variable to it with find/ or other commands?

推荐答案

_{} 如何将文件名分配给$1 涉及两件事.首先,find-exec 是如何工作的:它运行以下参数(直到转义的 ;)作为命令,但是{} 替换为它找到的文件的路径.因此,如果它找到 ./somefile.HEIC,它将运行等效的命令:

There are two things involved in how _ {} assigns the filename to $1. First, is how the find's -exec works: it runs the following arguments (up to the escaped ;) as a command, but with {} replaced with the path to the file it found. Thus, if it finds ./somefile.HEIC, it'll run the equivalent of the command:

sh -c 'magick convert $1 "${1%.HEIC}.JPG"' _ ./somefile.HEIC

第二部分是sh命令.sh 可以做很多事情,但如果给它一个 -c 选项,它会采用紧随其后的参数 (magick convert $1 "${1%.HEIC}.JPG") 作为一个命令字符串来解析和运行,有点像一个小脚本.之后的参数作为该迷你脚本的参数,以 $0 开头.在这种情况下,这意味着它运行迷你脚本,其中 $0 设置为 _$1 设置为./somefile.HEIC".如果提供更多参数,它们将是 $2$3 等.

The second part is the sh command. sh can do a number of things, but if it's given a -c option, it takes the immediately following argument (magick convert $1 "${1%.HEIC}.JPG") as a command string to parse and run, sort of like a little mini-script. The arguments after that are taken as arguments to that mini-script, starting with $0. In this case, that means it runs the mini-script with $0 set to _, and $1 set to "./somefile.HEIC". If more arguments were supplied, they'd be $2, $3, etc.

这篇关于有人可以解释一下这个 bash 末尾的符号的内部工作:“_ {} ;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆