在 bash 中搜索数组返回索引
只是 pesuocode 但这本质上就是我想做的。
Array=("1" "Linux" "Test system"
"2" "Windows" "Workstation"
"3" "Windows" "Workstation")
echo "number " ${array[search "$1"]} "is a" ${array[search "$1" +1]} ${array[search "$1" +2])}
这可以用 bash 实现吗?我只能找到有关搜索和替换的信息。我没有看到任何会返回和索引的东西。
Just pesuocode but this is essentially what I would like to do.
Array=("1" "Linux" "Test system"
"2" "Windows" "Workstation"
"3" "Windows" "Workstation")
echo "number " ${array[search "$1"]} "is a" ${array[search "$1" +1]} ${array[search "$1" +2])}
Is this possible with bash? I could only find info on search and replace. I didn't see anything That would return and index.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类似的事情应该可行:
虽然循环数组来查找索引当然是可能的,但这种使用关联数组的替代解决方案更实用:
Something like that should work:
While looping over the array to find the index is certainly possible, this alternative solution with an associative array is more practical:
您可以从此链接修改此示例,以轻松返回索引:
You could modify this example from this link to return an index without much trouble: