Bob Lee
Bob Lee

Reputation: 19

How to call Terminal Commands in a TEXT File?

I really need help on this, couldn't find much examples on google.

For example: I have this web project. Inside this project there is, app.js, views/home.ejs, some npm packages and etc...

This project, we are not allowed to type "npm install" "npm start" to run our project or we get a zero. The teacher will only type 1 line in terminal which is "run app.py" for python or "run js app" to run our code, will not install packages on localhost.

Wants us to make a textfile to automatically install all packages in the background and run the application automatically? How would I do that?

For example in text file:

inside TXT.FILE { #1 run "npm install express" in terminal

#2 run "npm install body-parser" in terminal

#3 run "node app.js" in terminal

#4 also run "ls" }

Basically just call terminal commands in a text file. A text file that will automatically execute them in order.

Upvotes: 0

Views: 369

Answers (2)

Bob Lee
Bob Lee

Reputation: 19

"scripts": { ... "start": "npm install express && npm install body-parser && node app.js && ls" },

Yes this works very well, tested it! Thank you.

This calls terminal commands under 1 line of code!

Upvotes: 0

daenax
daenax

Reputation: 26

You have two options.

  1. Add this into your package.json file
...
 "scripts": {
    ...
    "start": "npm install express && npm install body-parser && node app.js && ls"
  },

Now you can use npm start to run all these commands at one go.

  1. Add a bash script in your project directory. The file should be named your-script-name.sh. Inside add
#!/bin/bash
npm install express && npm install body-parser && node app.js && ls

You can run the script using ./your-script-name.sh in your terminal.

Upvotes: 1

Related Questions