[英]Zipping for loop in fish shell (loop over multiple lists)
I often write ad-hoc loops straight into the command-line to run a bunch of experiments.我经常在命令行中直接编写临时循环来运行一系列实验。 It's always a one-time thing, so writing a script file for it is unwanted overhead.
它始终是一次性的,因此为它编写脚本文件是不必要的开销。
Often, though, I'd like to zip over a bunch of parameters, I would like to run a command somewhat like the following:但是,通常我想压缩一堆参数,我想运行一个类似于以下的命令:
for arg1,arg2 in 256,lol 128,foo 32,bar
./bla --many-flags --name $arg2 --something $arg1
end
I can achieve something similar but quite brittle in fish with string
(or tr delim \\n
in old versions) like so:我可以用
string
(或旧版本中的tr delim \\n
)在鱼中实现类似但非常脆弱的东西,如下所示:
for exp in 256,lol 128,foo 32,bar
./bla --many-flags --name (string split ',' $exp)[2] --flag (string split ',' $exp)[1]
end
I'm wondering of anyone knows of better ways which don't require the cumbersome sub-command for each use of an argument (an argument may even be used multiple times), and even worse, the arbitrary delimiter which can cause all sorts of problems?我想知道有没有人知道更好的方法,这些方法不需要每次使用一个参数时都使用繁琐的子命令(一个参数甚至可能被多次使用),更糟糕的是,任意定界符会导致各种问题?
Ideally, I could even use it as a let
like so:理想情况下,我甚至可以使用它作为一个
let
像这样:
for arg1,arg2 in 256,lol
./bla --many-flags --ame $arg2 --something $arg1
end
As per the comments on the question, this is not supported.根据对该问题的评论,这是不支持的。 The closest workaround seems to be:
最接近的解决方法似乎是:
for exp in 256,lol 128,foo 32,bar
echo $exp | read -d , arg1 arg2
./bla --many-flags --name $arg2 --flag $arg1
end
Not quite satisfying for regular interactive use, but I'll survive it I guess.对于常规的交互式使用来说不太令人满意,但我想我会活下来的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.