简体   繁体   English

命令在脚本中失败,在命令行中有效

[英]Command fails in script, works in command line

I have a simple script to get an export from MongoDB: 我有一个简单的脚本可以从MongoDB导出数据:

#!/bin/sh -x

QUERY="'{ \"type\":\"listing\" }'"

mongoexport --db event --collection listing --query $QUERY --fields type,name --csv

(I'm using the -x switch for debugging purposes) (我将-x开关用于调试目的)

Here's the output when I run the script: 这是我运行脚本时的输出:

$ ./simple_query.sh 
+ QUERY='{ "type":"listing" }'
+ mongoexport --db event --collection listing --query '{ "type":"listing" }' --fields type,name --csv
ERROR: too many positional options

However, if I merely copy and paste the mongoexport line to the prompt, it works! 但是,如果我仅将mongoexport行复制并粘贴到提示中,它将起作用! What's going on? 这是怎么回事?

(Using MongoDB 2.0.0) (使用MongoDB 2.0.0)

Are you using older version of mongodb? 您是否正在使用旧版本的mongodb? If so maybe it's this bug. 如果是这样,也许就是这个错误。 It is fixed on 2.0.0-* versions. 它已在2.0.0- *版本上修复。

Edit : Actually this might be different problem. 编辑 :实际上这可能是不同的问题。 You can fix it by removing the spaces on the query. 您可以通过删除查询中的空格来修复它。 Spaces seem to make the shell think it is actually multiple arguments: 空格似乎使shell认为它实际上是多个参数:

QUERY="'{\"type\":\"listing\"}'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM