having fun with code

Command line scripting with Node.js

I often write custom deploy scripts for the projects I work on. In most cases, I choose python. You can do pretty much everything with shell and system commands, but python dictionaries and lists make it very easy to manipulate data, lists of files, text, etc. Networking is also very easy in python (http, ftp, etc).

To run a Javascript file from the console you can type “node myscript.js”. Or you can set the application that will run the file like you do with bash or python:

  1. #! /usr/bin/env node
  2.  
  3. console.log("Hello");
  4. setTimeout(function() {
  5.   console.log("world!");
  6. }, 2000);

Now you can change the permissions of the script to make it executable:

  1. $ chmod 755 myscript.js

And run the script by typing its name, like you do with shell or python scripts:

  1. $ ./myscript.js param1 param2 "param 3"
  2. Hello
  3. world!

Nice, uh? Now go and write some cool command line scripts in Javascript :)

Related Posts:

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info

PromoteJS

JavaScript JS Documentation