将变量插入bash脚本mongodb命令heredoc

时间:2014-02-25 20:58:51

标签: bash mongodb

我在bash脚本中运行mongodb的一些命令,但需要在heredoc文本中插入一个字符串。我无法正确插入值。怎么会这样做?

today=`date -d "00:00:00" +%s`
todaytime=$(($today*1000))
mongo <<EOF > test
    use log
    db.translogs.remove("{Dt: {$lt: new Date($todaytime)}}")
    exit
EOF

1 个答案:

答案 0 :(得分:0)

我不知道,但它看起来像$lt is part of a query而不是shell变量。但是在你的代码中,shell在将here-doc传递给mongo之前尝试扩展它。所以我认为您需要做的就是逃避$中的$lt

today=`date -d "00:00:00" +%s`
todaytime=$(($today*1000))
mongo  test
    use log
    db.translogs.remove("{Dt: {\$lt: new Date($todaytime)}}")
    exit
EOF

如果我在上面的代码中将mongo替换为cat,我们会在mongo文件中看到要发送到test的命令:

    use log
    db.translogs.remove("{Dt: {$lt: new Date(1393315200000)}}")
    exit