0

I'm running shell script file from nodeJS file i need to send some parameters to shell script and use it in shell script

My js file

var dbname = development.db.uri;
    console.log(dbname);
    var myscript = exec('sh ./scripts/mongo-backup.sh'+ dbname );
    myscript.stdout.on('data',function(data){
        console.log("!@#$%^&*&@#$%^&*$##$%^&*")
        console.log(data); // process output will be displayed here
    });
    myscript.stderr.on('data',function(data){
        console.log(data); // process error output will be displayed here
    });

and here is my shell script

#!/bin/bash
echo "Creating Database Backup"
#need to access dbname from JS FILE
MONGO_DATABASE= dbname 
APP_NAME="app-name"
USERNAME=$(sudo echo $USER)
3
  • ,+ is invalid Commented May 19, 2017 at 8:35
  • @GottZ it is incorrect but valid, it turns it into a NaN, assuming its not a number or a string with number inside Commented May 19, 2017 at 8:39
  • oh derp.. you are right. ofc it can be used to convert anything into numbers Commented May 20, 2017 at 17:49

2 Answers 2

1

although it might be possible, the code mixture looks counter intuitive. I suggest having an environment variable setup for the dbname, both nodejs will capture it with process.env.dbname and the bash script will capture it with $dbname.

if you insist on the current structure, simply do this:

var myscript = exec('DB_NAME='+ dbname + ' DB_USER=' + dbuser +' sh ./scripts/mongo-backup.sh');

if you are using a fairly modern node, you can use this syntax:

var myscript = exec(`DB_NAME=${dbname} DB_USER=${dbuser} sh ./scripts/mongo-backup.sh`);

and in the bash file

#!/bin/bash
echo "Creating Database Backup"
MONGO_DATABASE= $DB_NAME 
APP_NAME="app-name"
USERNAME=$(sudo echo $USER)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Ahmad its working for me But i need to send more parameter like username and password i tried this var myscript = exec('DB_NAME='+dbname + 'DB_USER='+dbuser + ' sh ./scripts/mongo-backup.sh') but it is not working as expected any suggestions?
@Srinivas you have to add a space before DB_USER, it should work. updated the answer.
1

This will not work at all:

var myscript = exec('sh ./scripts/mongo-backup.sh',+ dbname );

because it tries to make a number from the dbname (the unary plus operator) which most likely results in NaN and then it passes is as the second argument to exec (which should be an optional options object).

What you probably meant is:

var myscript = exec('sh ./scripts/mongo-backup.sh ' + dbname);

Another thing to note is that your script is declared as a Bash script and you are explicitly running it with sh and not bash. The sh command on some system can be not compatible with Bash - e.g. sometimes it is Dash or some other simple shell, so this is better:

var myscript = exec('bash ./scripts/mongo-backup.sh ' + dbname);

This will pass it as an argument to the script which you will be able to access as $1 in the shell script:

#!/bin/bash
echo $1

or similar to your example:

#!/bin/bash
MONGO_DATABASE=$1
# use $MONGO_DATABASE as you need

You can do a similar thing with spawn without using string concatenation:

var myscript = spawn('bash', ['./scripts/mongo-backup.sh', dbname]);

This will not break when you have some character in dbname that would be misinterpreted by the shell as some special character. This is not likely but needs to be considered.

You can also pass your variables as environment variables to your shell script, so that you can use a named variables directly. For example:

var env = Object.assign({}, process.env, { dbname });
var myscript = spawn('bash', ['./scripts/mongo-backup.sh'], { env });

And now you can just use $dbname in your shell script, for example:

#!/bin/bash
echo $dbname

or similar to your example:

#!/bin/bash
MONGO_DATABASE=$dbname
# use $MONGO_DATABASE as you need

You can also pass the MONGO_DATABASE directly:

var env = Object.assign({}, process.env, { MONGO_DATABASE: dbname });
var myscript = spawn('bash', ['./scripts/mongo-backup.sh'], { env });

and just use it in your script with no other variables needed:

#!/bin/bash
echo $MONGO_DATABASE

You can pass data to your shell scripts with either environment variables or with parameters. Sometimes one is more convenient, sometimes the other.

See the docs for more info:

Update

You can send more then one argument with spaces to use $1, $2 and $3:

var myscript =
  exec('bash ./scripts/mongo-backup.sh ' + dbname + ' ' + dbuser + ' ' + dbpass);

Same as above but with backticks, to use $1, $2 and $3:

var myscript =
  exec(`bash ./scripts/mongo-backup.sh ${dbname} ${dbuser} ${dbpass}`);

Same as above but without string concatenation, to use $1, $2 and $3:

var myscript =
  spawn('bash', ['./scripts/mongo-backup.sh', dbname, dbuser, dbpass]);

Or passed as environment variables, to use as $dbname $dbuser $dbpass:

var env = Object.assign({}, process.env, { dbname, dbuser, dbpass });
var myscript = spawn('bash', ['./scripts/mongo-backup.sh'], { env });

2 Comments

can i send more than one argument with spaces like i've dbname, dbuser dbpass var myscript = exec('bash ./scripts/mongo-backup.sh ' + dbname dbuser dbpass); or is there any other method
@Srinivas You can do it for multiple variables but with spaces and + you need to add plus between all elements and add spaces between. See my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.