csh 脚本 foreach 循环中的多个单词列表

发布于 2024-09-19 04:46:47 字数 514 浏览 5 评论 0原文

我有一个 Cshell 脚本,我正在修改它以具有相关的输入和输出位置。 所有功能都发生在 foreach 循环中,如下所示:

set INPUT_LOCATION_LIST = "loc1 loc2 loc3 loc4"
foreach location ($INPUT_LOCATION_LIST)
#***Do some stuff ***
end

我希望有一个与输入列表具有不同值的输出列表,但在 foreach 循环的每次迭代中都遍历它。 foreach 的 man 只是

foreach name (wordlist)

定义了。所以只处理一个。我目前处理它的想法是让单词列表包含输入和输出位置,然后在脚本中解析它:

set INPUT_LOCATION_LIST = "loc1;out1 loc2;out2 loc3;out3 loc4;out4"

所以我想知道是否有人有更好的方法来做到这一点。

I have a Cshell script that I am modifying to have related input and output locations.
the functionality all happens in a foreach loop like so:

set INPUT_LOCATION_LIST = "loc1 loc2 loc3 loc4"
foreach location ($INPUT_LOCATION_LIST)
#***Do some stuff ***
end

I would like to have an output list with different values than the input list but traverse through it each iteration through the foreach loop. The man for foreach simply has

foreach name (wordlist)

as the definition. so only dealing with a single one. my current thought on dealing with it is have the wordlist contain both input and output location and then parse it out in the script:

set INPUT_LOCATION_LIST = "loc1;out1 loc2;out2 loc3;out3 loc4;out4"

so im wondering if anyone has a better way to do that.

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

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

发布评论

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

评论(2

尾戒 2024-09-26 04:46:48

我通常不使用 csh,但你的问题引起了我的注意。可能有一个步骤更少的解决方案,但这种事情在我的 csh 版本中有效:

foreach location ($INPUT_LOCATION_LIST)
    set one_word_with_space = ${location:s/;/ /}
    set loc_out = ($one_word_with_space)
    set loc = ${loc_out[1]}
    set out = ${loc_out[2]}
    ...
end

基本思想是将分号分隔的字符串更改为空格分隔的字符串,然后将其解析为数组。

I don't normally use csh, but your question caught my eye. There's probably a solution with less steps, but this kind of thing worked in my version of csh:

foreach location ($INPUT_LOCATION_LIST)
    set one_word_with_space = ${location:s/;/ /}
    set loc_out = ($one_word_with_space)
    set loc = ${loc_out[1]}
    set out = ${loc_out[2]}
    ...
end

Basic idea is just to change the semi-colon separated string into a space-separated string, then parse that into an array.

青衫负雪 2024-09-26 04:46:47

您可以使用 foreach 迭代一个列表,并通过将 at 视为数组并使用 shift 迭代另一个列表:

set INPUT_LOCATION_LIST = "loc1 loc2 loc3 loc4"
set OUT_LIST = (out1 out2 out3 out4)

foreach location ($INPUT_LOCATION_LIST)
    do_something $location $OUT_LIST[1]
    shift OUT_LIST
end

You can iterate through one list using foreach and through the other one by treating at like an array and using shift:

set INPUT_LOCATION_LIST = "loc1 loc2 loc3 loc4"
set OUT_LIST = (out1 out2 out3 out4)

foreach location ($INPUT_LOCATION_LIST)
    do_something $location $OUT_LIST[1]
    shift OUT_LIST
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文