如何将输出作为参数传递给另一个命令?

问题描述:

我使用的是命令,如何将输出作为参数传递给另一个命令?

gsettings2 monitor org.gnome.desktop.background picture-uri| cut -f2 -d "'" 

这给正确的URI改变壁纸。 我想管每一个这样的价值函数foo这样

function foo { 
    echo "Value changed $1" 
} 

执行。我该怎么做?

gsettings2 ... | stdbuf -oL cut -f2 -d "'" | while read -r uri; do 
    foo "$uri" 
done 

while read环路读取每个URI调用foostdbuf -oL调用在那里强制cut被行缓冲,因此其输出立即可见。

+0

嗨,谢谢你的回复。我自己尝试这个,但由于某种原因,这不起作用。更新的问题。请注意[gsettings monitor](https://developer.gnome.org/gio/stable/gsettings-tool.html)本身就是一个无限循环。 – prakharsingh95 2014-10-08 21:19:31

+0

我不明白的是,如果切割工作,为什么不是这样。 – prakharsingh95 2014-10-08 21:26:13

+0

如果你执行'stdbuf -oL cut -f2 -d''“'有帮助吗?问题可能是'cut'的输出是完全缓冲的。如果stdbuf帮助让我知道,我会将其添加到我的答案。 – 2014-10-08 22:19:33