zsh 运行 多次命令正则表达式的结果

zsh run command multiple times for results of regular expression

我想 运行 一个命令多次,这取决于我在 zsh 中的正则表达式的结果。

这是我希望盒子执行的操作的示例:

Try1:ping -c 1 '10.0.0.4[4-6]' 给我 ping: 10.0.0.4[4-6]: Name or service not known

尝试 2:ping -c 1 10.0.0.4{4..6} 仅 ping 10.0.0.46

如何正确执行此操作?

您可以采用两种方法:

% eval 'ping -c1 10.0.0.4'{4..6}';'  # needs ; to separate the commands
% for ip in 10.0.0.4{4..6}; do ping -c1 $ip; done

两者将产生相同的输出。

在脚本或函数中,我通常更喜欢后者,因为它速度稍快,并且 shell 能够在解析时检测语法错误,但前者输入速度更快。