击:不能在管道的右侧建阵列 [英] Bash: can't build array in right side of pipe

查看:89
本文介绍了击:不能在管道的右侧建阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有一个线索,为什么预期该code不工作?

  $> svnTags =()
$> SVN LS HTTP://plugins.svn.word$p$pss.org/duplicate-post/tags/ |而读线; DO slashless = $(SED的#/ $ ## G'<<< $线);回声slashless - $ slashless svnTags + =($ slashless); DONE
slashless - 1.0
slashless - 1.1
slashless - 1.1.1
slashless - 1.1.2
slashless - 2.0
slashless - 2.0.1
slashless - 2.0.2
slashless - 2.1
slashless - 2.1.1
slashless - 2.2
slashless - 2.3
$>回声$ svnTags [@]

不给予任何输出,我期待它输出SVN标签内置阵列。

第二个命令爆发了:

  SVN LS HTTP://plugins.svn.word$p$pss.org/duplicate-post/tags/ |而读线;做
    slashless = $(SED的#/ $ ## G'<<< $线)
    回声slashless - $ slashless
    svnTags + =($ slashless)
DONE


解决方案

由于后会发生什么| 是一个子shell。在子shell变量改变不传播回父shell。

常见解决方法:

 而读线;做
    ...
完成< ≤(SVN LS的http:// ...)

Anyone have a clue as to why this code is not working as expected?

$> svnTags=()
$> svn ls http://plugins.svn.wordpress.org/duplicate-post/tags/ | while read line; do slashless=$(sed 's#/$##g' <<< $line); echo "slashless - $slashless"; svnTags+=($slashless); done
slashless - 1.0
slashless - 1.1
slashless - 1.1.1
slashless - 1.1.2
slashless - 2.0
slashless - 2.0.1
slashless - 2.0.2
slashless - 2.1
slashless - 2.1.1
slashless - 2.2
slashless - 2.3
$> echo "$svnTags[@]"

Not giving any output, I'm expecting it to output the built array of the svn tags.

Second command broken out:

svn ls http://plugins.svn.wordpress.org/duplicate-post/tags/ | while read line; do
    slashless=$(sed 's#/$##g' <<< $line)
    echo "slashless - $slashless"
    svnTags+=($slashless)
done

解决方案

Because what happens after | is a subshell. Variables changed in a subshell do not propagate back to the parent shell.

Common workaround:

while read line ; do
    ...
done < <(svn ls http://...)

这篇关于击:不能在管道的右侧建阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆