Solaris 上的 Bash 脚本,使用“:”在数组上并不总是有效
我在 Solaris 上的 bash 脚本中遇到了一个奇怪的数组操作问题。我使用语法 ${varName[@]:index}
来获取数组 varname 中指定索引之后的所有元素。但是,如果指定索引后只有一个元素,则不会返回任何内容。
这可以通过示例轻松演示:
#!/bin/bash
paramArray=( a b c )
echo "everything after 2" ${paramArray[@]:2} # Should display c but doesn't
echo "parameter 2 only " ${paramArray[2]} # Correctly displays c
paramArray=( a b c d e )
echo "everything after 2" ${paramArray[@]:2} # Correctly displays c d e
echo "parameter 2 only " ${paramArray[2]} # Correctly displays c
此代码在运行 Cygwin 的 Windows 机器上可以正常工作,但在 Solaris 上失败(版本:Solaris 9 9/05 s9s_u8wos_05 SPARC)
任何人都可以解释这种行为吗?
I have a strange issue with array manipulation within a bash script on Solaris. I am using the syntax ${varName[@]:index}
to obtain all of the elements in array varname after the specified index. However, if there is only one element after the specified index, nothing is returned.
This can be easily demonstrated by example:
#!/bin/bash
paramArray=( a b c )
echo "everything after 2" ${paramArray[@]:2} # Should display c but doesn't
echo "parameter 2 only " ${paramArray[2]} # Correctly displays c
paramArray=( a b c d e )
echo "everything after 2" ${paramArray[@]:2} # Correctly displays c d e
echo "parameter 2 only " ${paramArray[2]} # Correctly displays c
This code works correctly on a Windows box running Cygwin, but fails on Solaris (version: Solaris 9 9/05 s9s_u8wos_05 SPARC)
Can anyone explain this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这听起来像是 Solaris 系统上的 Bash 版本中的错误。
您的两个系统都运行什么版本(
bash --version
)?您可以查看 CHANGELOG 并搜索在Solaris 版本的发布以及 Cygwin 版本的发布之前。
That sounds like a bug in the version of Bash on your Solaris system.
What versions (
bash --version
) are you running both your systems?You might look through the CHANGELOG and search for array bugs that are fixed after the release of your Solaris version and before the release of your Cygwin version.