我试图通过拆分变量来初始化读取数组.
x=abc:def:gh
declare -a xa
# I want xa[0] = abc, xa[1] = def, and so on
IFS=: read -a xa <<< $x
echo ${#xa[@]} $xa ######### the above did not work
1 abc def gh
# but type the same value from console?
IFS=: read -a xa
abc:def:gh ########## this is what I typed in
echo ${#xa[@]} $xa ######### this worked
3 abc
Run Code Online (Sandbox Code Playgroud)
使用<<<读取变量时如何使IFS工作?
非常感谢您的建议.
此外,这是我的实际问题,以防万一有更智能的解决方案.我使用SVN,不同的人有兴趣知道不同的路径集.在SVN提交后,我想过滤更改列表,并根据他们的愿望向不同的人群发送电子邮件.所以我想我会在hooks-env中设置类似下面的内容
NOTIFY_LIST=mailinglist:grep options:grep options:......
Run Code Online (Sandbox Code Playgroud)
然后,在post-commit中,解析svnlook数据以查看是否有任何候选电子邮件.有没有一种声明性的方式可以说这种和这样的路径的变化对这样的人员名单有意义?
谢谢Dinesh
编辑:尝试组合IFS和简单xa=($x).因此看起来IFS=: 不能与阅读合并.所以我有办法完成我的工作,但仍然好奇发生了什么?
IFS=: xa=($x) # the array is populated as expected
IFS=b xa=($x) # the array is populated as expected
Run Code Online (Sandbox Code Playgroud)
再次感谢.
使用更多报价!
IFS=: read -r -a xa <<<"$x"
Run Code Online (Sandbox Code Playgroud)
......按预期工作.
同样,如果要准确地查看数组的内容,echo则是错误的工具,尤其是在不引用其参数时.请考虑一下:
printf '%q\n' "${xa[@]}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
684 次 |
| 最近记录: |