命令在脚本中失败,在命令行中工作 [英] Command fails in script, works in command line

查看:199
本文介绍了命令在脚本中失败,在命令行中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的脚本从MongoDB导出:

I have a simple script to get an export from MongoDB:

#!/bin/sh -x

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

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

(我使用-x开关进行调试)

(I'm using the -x switch for debugging purposes)

这是我运行脚本时的输出:

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

,如果我只是复制和粘贴mongoexport行到提示符,它的工作原理!发生了什么事?

However, if I merely copy and paste the mongoexport line to the prompt, it works! What's going on?

(使用MongoDB 2.0.0)

(Using MongoDB 2.0.0)

推荐答案

您使用的是旧版本的mongodb吗?如果是这样,则可能是错误。它是固定的2.0.0- *版本。

Are you using older version of mongodb? If so maybe it's this bug. It is fixed on 2.0.0-* versions.

编辑:这可能是不同的问题。您可以通过删除查询上的空格来解决它。空格似乎使shell认为它实际上是多个参数:

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:

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

这篇关于命令在脚本中失败,在命令行中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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