首页 > 解决方案 > 遍历鱼壳中的字母

问题描述

在 bash 中,我可以:

for i in /dev/sd{b..g}; do pvcreate $i; done

在鱼中寻找与此循环等效的内容。

for i in /dev/sd{b..g}
    pvcreate $i
end

返回设备 /dev/sdb..g 未找到(或被过滤忽略)。错误

标签: fish

解决方案


没有直接翻译:fish 只在大括号内使用逗号分隔的元素——https: //fishshell.com/docs/current/index.html#expand-brace

你也不能这样做for i in /dev/sd[b-g],因为fish在通配符中不使用字符集作为通配符——https: //fishshell.com/docs/current/index.html#expand-wildcard

您最终会调用一些外部程序来生成该文件列表,并且您知道 bash 有效,因此:

for i in (bash -c 'printf "%s\n" /dev/sd{b..g}'); ...

这是fish的设计原则之一的示例:

可以在其他 shell 语言中完成的所有事情都应该可以在 fish 中完成,尽管 fish 在这样做时可能依赖外部命令。


推荐阅读