1

I want to write a .sh file to run multiple javascript files.

I know that I need the following lines:

#!/usr/bin/env node
chmod u+x ./a/file1.js ./a/file2.js

Is it correct to put two .js files one after the other? I need file1.js to execute first and then file2.js because some of the functions from file2.js need information outputted from file1.js.

I'm also a bit confused with the difference between #!/usr/bin/env node and #!/usr/bin/env bash. Will I still be able to run my .js files with bash?

2 Answers 2

1

#!/usr/bin/env node sets the environment to Node.js which is a JavaScript environment.

For example if you create a simple script test.js with the following content:

#!/usr/bin/env node

// now we're in JavaScript!
var d = new Date();

console.log('Current Time: ' + d);

Afterwards you make it executable:

$ chmod u+x test.js

Then you can execute it in the shell:

$ test.js
Current Time: Sun Nov 13 2016 07:14:15 GMT+0000 (GMT)

The file extension doesn't matter:

$ mv test.js test.sh
$ test.sh
Current Time: Sun Nov 13 2016 07:15:05 GMT+0000 (GMT)

If you don't want to change your JavaScript files so that they become shell scripts then you can run them from a shell script using Node.

tasks.sh:

#!/bin/sh
node ./a/file1.js
node ./a/file2.js

Afterwards make it executable and run it:

$ chmod u+x tasks.sh
$ tasks.sh
Sign up to request clarification or add additional context in comments.

3 Comments

I want to run the tasks.sh file with as little work as possible.
Sorry I submitted my response too early. Can I include chmod u+x in my tasks.sh file so that I do not include it in the command line? Also, can I make this: #!/usr/bin/env bash my first line of the .sh file and still run node?
The command chmod u+x gives the user (owner) executable permissions for the file. You only need to do this once. #!/usr/bin/env bash as the first line of the script file specifies that the script is to use the bash interpreter, you can still run node within the script.
1

How about:

#!/bin/sh
node ./a/file1.js
node ./a/file2.js

Because:

#!/usr/bin/env node causes the program node to be run passing this script as its first argument. #!/usr/bin/env bash causes bash to be run. Since the script is a bash script, you want the latter.

3 Comments

#!/usr/bin/env node causes the program node to be run passing this script as its first argument. #!/usr/bin/env bash causes bash to be run. Since the script is a bash script, you want the latter.
Thank you for the clarification. I think I will write out my script as: #!/usr/bin/env bash node ./a/file1.js node ./a/file2.js Do you know if chmod u+x should also be part of my shell script? What is the use of it?
chmod should not be part of the script. It's something you'd typically do just one time after creating the script. You don't need to chmod the *.js files, only the shell script.

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.