shell 中棘手的支撑扩展

发布于 2024-07-19 05:51:09 字数 543 浏览 5 评论 0原文

使用 POSIX shell 时,以下

touch {quick,man,strong}ly

内容扩展为

touch quickly manly strongly

Which will touch the files quicklymanlystrongly,但是是否可以动态创建扩张? 例如,以下说明了我想要执行的操作,但由于扩展顺序而不起作用:

TEST=quick,man,strong    #possibly output from a program
echo {$TEST}ly

有什么方法可以实现此目的吗? 如果需要的话,我不介意将自己限制在 Bash 上。 我还想避免循环。 扩展应该作为任意程序的完整参数给出(即该程序不能为每个文件调用一次,只能为所有文件调用一次)。 我了解 xargs,但我希望这一切都可以以某种方式从 shell 完成。

When using a POSIX shell, the following

touch {quick,man,strong}ly

expands to

touch quickly manly strongly

Which will touch the files quickly, manly, and strongly, but is it possible to dynamically create the expansion? For example, the following illustrates what I want to do, but does not work because of the order of expansion:

TEST=quick,man,strong    #possibly output from a program
echo {$TEST}ly

Is there any way to achieve this? I do not mind constricting myself to Bash if need be. I would also like to avoid loops. The expansion should be given as complete arguments to any arbitrary program (i.e. the program cannot be called once for each file, it can only be called once for all files). I know about xargs but I'm hoping it can all be done from the shell somehow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

对你的占有欲 2024-07-26 05:51:09

...使用eval有很多错误。 您所要求的只能通过eval实现,但是您可能想要的东西很容易实现,而无需诉诸bash bug-central。

使用数组! 每当您需要将多个项保存在一个数据类型中时,您就需要(或者应该使用)一个数组。

TEST=(quick man strong)
touch "${TEST[@]/%/ly}"

这正是您想要的,没有此处其他建议中引入和隐藏的数千个错误和安全问题。

它的工作方式是:

  • "${foo[@]}":通过扩展名为 foo 的数组,并正确引用它的每个元素来扩展该数组。 不要忘记引号!
  • ${foo/a/b}:这是一种参数扩展,它将 foo 扩展中的第一个 a 替换为 <代码>b。 在这种类型的扩展中,您可以使用 % 来表示扩展值的结尾,有点像正则表达式中的 $
  • 将所有这些放在一起,“${foo[@]/%/ly}”将展开 foo 的每个元素,将其正确引用为单独的参数,并将每个元素的结尾替换为 ly

... There is so much wrong with using eval. What you're asking is only possible with eval, BUT what you might want is easily possible without having to resort to bash bug-central.

Use arrays! Whenever you need to keep multiple items in one datatype, you need (or, should use) an array.

TEST=(quick man strong)
touch "${TEST[@]/%/ly}"

That does exactly what you want without the thousand bugs and security issues introduced and concealed in the other suggestions here.

The way it works is:

  • "${foo[@]}": Expands the array named foo by expanding each of its elements, properly quoted. Don't forget the quotes!
  • ${foo/a/b}: This is a type of parameter expansion that replaces the first a in foo's expansion by a b. In this type of expansion you can use % to signify the end of the expanded value, sort of like $ in regular expressions.
  • Put all that together and "${foo[@]/%/ly}" will expand each element of foo, properly quote it as a separate argument, and replace each element's end by ly.
也只是曾经 2024-07-26 05:51:09

在 bash 中,您可以执行以下操作:

#!/bin/bash
TEST=quick,man,strong
eval echo $(echo {$TEST}ly)
#eval touch $(echo {$TEST}ly)

最后一行被注释掉,但会触及指定的文件。

In bash, you can do this:

#!/bin/bash
TEST=quick,man,strong
eval echo $(echo {$TEST}ly)
#eval touch $(echo {$TEST}ly)

That last line is commented out but will touch the specified files.

场罚期间 2024-07-26 05:51:09

Zsh 可以轻松做到这一点:

TEST=quick,man,strong
print ${(s:,:)^TEST}ly

变量内容以逗号分隔,然后每个元素分布到大括号周围的字符串中:

quickly manly strongly

Zsh can easily do that:

TEST=quick,man,strong
print ${(s:,:)^TEST}ly

Variable content is splitted at commas, then each element is distributed to the string around the braces:

quickly manly strongly
眼藏柔 2024-07-26 05:51:09

从上面的答案中汲取灵感:

$ TEST=quick,man,strong
$ touch $(eval echo {$TEST}ly)

Taking inspiration from the answers above:

$ TEST=quick,man,strong
$ touch $(eval echo {$TEST}ly)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文