加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在bash中的元组上循环?

发布时间:2020-12-15 16:21:00 所属栏目:安全 来源:网络整理
导读:是否可以在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; donec and 3e and 5
是否可以在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读