从MongoDB中的shell脚本中使用结果 [英] Use result from mongodb in shell script

查看:1063
本文介绍了从MongoDB中的shell脚本中使用结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用从一个bash脚本参数化的MongoDB脚本文件打印结果。

I am trying to use the result printed from a parameterized MongoDB script file in a bash script.

调用看起来是这样的:

mongo --quiet server/db --eval "a='b'" mongoscript.js

里面mongoscript.js还有就是打印值'富'我想在我的shell脚本使用打印语句。问题是,当我执行上面的语句,我得到:

Inside mongoscript.js there is a print statement that prints the value 'foo' I want to use in my shell script. The problem is that when I execute above statement I get:

b
foo

而不仅仅是'富'

instead of just 'foo'

因此​​,如果我这样做

Thus, if I do

res=`mongo --quiet server/db --eval "a='b'" mongoscript.js`

水库包含行。

我当然可以用

res=`mongo ... |tail -n 1`

但我希望有避免这种多余的输出更普遍的方式。

but I am hoping there is a more general way to avoid this superfluous output.

谢谢!

推荐答案

多余的输出是你的分配结果 A ='B',显示结果在这方面的分配

The superfluous output is the result of your assignment of a='b', which displays the result of the assignment in this context.

如果您添加 VAR 关键字为变量赋值,你不应该有任何额外的输出(并且仍​​然可以使用变量 A 在你的脚本):

If you add the var keyword for variable assignment, you shouldn't have any extra output (and can still use the variable a in your script):

$ mongo --quiet --eval "var a='b'" mongoscript.js
foo

您可以看到在蒙戈外壳相同的行为:

You can see the same behaviour in the mongo shell:

> a='b'
b
> var a='b'
>

这篇关于从MongoDB中的shell脚本中使用结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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