Will
Will

Reputation: 8641

Passing a variable to a js script from a shell script

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

Upvotes: 2

Views: 5588

Answers (2)

san8055
san8055

Reputation: 373

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

Upvotes: 1

ShaneQful
ShaneQful

Reputation: 2236

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

Upvotes: 0

Related Questions