2

I have the following shell script, that kicks off a js script against mongo db I wish to pass a variable to the js file. A second caveat is that I need to store this variable somewhere on the unix box to as the last run time of the script. Any help or pointers is appreciated.

# check if previous job still running
if [ -f /tmp/mapreduce_compound.lck ]
then
    exit
else
   # if no lock file present, create one
   touch /tmp/mapreduce_compound.lck
fi

mongo -u xxx mongo1.pilot.dice.com:27017/tracking /usr/local/gemini/mongodb/tracking/mapReduceFunctionsByGroupIdIterative.js > /tmp/mapReduceFunctionsByGroupIdIterative.txt 2>&1


#remove lock file
rm /tmp/process_nightly.lck
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Will
  • 8,246
  • 16
  • 60
  • 92
  • possible duplicate of [How to pass argument to Mongo Script](http://stackoverflow.com/questions/10114355/how-to-pass-argument-to-mongo-script) – ShaneQful Feb 19 '15 at 20:11

2 Answers2

1

To pass additional variables to the javascript file use process.argv. Here is the link to node js documentation.

san8055
  • 363
  • 6
  • 13
0

You can use

--eval 'var param="$yourparam";'

to pass in arguments.

Note: this isn't passing in arguments so much defining variables your script can use but it does the trick.

So if you wanted to pass the first argument for your shell script in you could do something like this:

# check if previous job still running
if [ -f /tmp/mapreduce_compound.lck ]
then
    exit
else
   # if no lock file present, create one
   touch /tmp/mapreduce_compound.lck
fi

MONGOARG=$1
echo $MONGOARG > lastargcalled

mongo -u xxx --eval 'var param="$MONGOARG";' mongo1.pilot.dice.com:27017/tracking /usr/local/gemini/mongodb/tracking/mapReduceFunctionsByGroupIdIterative.js > /tmp/mapReduceFunctionsByGroupIdIterative.txt 2>&1


#remove lock file
rm /tmp/process_nightly.lck
ShaneQful
  • 2,140
  • 1
  • 16
  • 22