开发者

Multiple wordlists in csh script foreach loop

开发者 https://www.devze.com 2023-01-15 23:24 出处:网络
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:

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 wordlis开发者_Python百科t 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消