2014-02-25 97 views
1

我正在bash腳本中運行一些mongodb命令,但需要在heredoc文本中插入一個字符串。我無法正確插入值。這將如何完成?將變量插入到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 

回答

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 

如果我在上面的代碼cat更換mongo,我們看到了命令,要被送到mongotest文件:

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