gpt4 book ai didi

bash - 在 shell 脚本中使用 mongodb 的结果

转载 作者:IT老高 更新时间:2023-10-28 13:27:44 26 4
gpt4 key购买 nike

我正在尝试在 bash 脚本中使用从参数化 MongoDB 脚本文件打印的结果。

调用如下所示:

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

在 mongoscript.js 中有一个 print 语句,用于打印我想在我的 shell 脚本中使用的值 'foo'。问题是当我执行上述语句时,我得到:

b
foo

而不仅仅是“foo”

如果我这样做了

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

res 包含两行。

我当然可以用

解决这个问题
res=`mongo ... |tail -n 1`

但我希望有一种更通用的方法来避免这种多余的输出。

谢谢!

最佳答案

多余的输出是你对 a='b' 赋值的结果,它在这个上下文中显示了赋值的结果。

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

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

您可以在 mongo shell 中看到相同的行为:

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

关于bash - 在 shell 脚本中使用 mongodb 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21553535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com