微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在bash中的元组上循环?

是否可以在bash中循环元组

作为一个例子,如果以下工作将是巨大的:

for (i,j) in ((c,3),(e,5)); do echo "$i and $j"; done

一个解决方法,不知何故让我循环的元组

$ for i in c,3 e,5; do IFS=","; set -- $i; echo $1 and $2; done
c and 3
e and 5

关于这个使用set(from man builtins):

Any arguments remaining after option processing are treated as values
for the positional parameters and are assigned,in order,to $1,$2,
… $n

IFS =“,”设置字段分隔符,因此每个$ i都被正确分段为$ 1和$ 2。

通过this blog

编辑:更正确的版本,如@SLACEDIAMOND建议:

$ OLDIFS=$IFS; IFS=','; for i in c,5; do set -- $i; echo $1 and $2; done; IFS=$OLDIFS
c and 3
e and 5

原文地址:https://www.jb51.cc/bash/390866.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐