我有一个名称列表,并且有一个二进制文件。我想复制该二进制文件,以便列表的每个成员都有一个副本。该列表是一个文本文件,每行有一个名称。我一直回到
for i in $(cat ../dir/file); do cp binaryfile.docx "$i_binaryfile.docx"; done
没有错误。仅创建一个名为_binaryfile.docx的文件。
我已经看过了[将文件复制到具有不同名称的目标]和[将文件复制到命令外壳的x倍次数],但是我看不到它们有何不同。
它应该是:
for i in $(cat file); do cp binaryfile.docx "${i}_binaryfile.docx"; done
编辑:
您可以通过以下示例重现它:
$ i=1
$ echo $i
1
$ echo $i_7
$ echo ${i}_7
1_7
关键是变量名中允许使用_
(下划线)字符。您可以阅读其中的内容,man bash
但请记住,它是使用非常技术性,简洁的语言编写的:
name A word consisting only of alphanumeric characters and underscores, and
beginning with an alphabetic character or an underscore. Also referred to
as an identifier.
然后:
A variable is a parameter denoted by a name.
和:
${parameter}
The value of parameter is substituted. The braces are required when
parameter is a positional parameter with more than one digit, or when
parameter is followed by a character which is not to be interpreted as
part of its name. The parameter is a shell parameter as described above
PARAMETERS) or an array reference (Arrays).
因此,如果我们有一个名为的变量i
,并且想将其值打印在相邻变量的旁边,_
则必须将其括起来{}
以告诉Bash
该变量的名称在_
。之前结束。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句