dreamsys software

JavaScript Shell Scripting Tutorial

Introduction

JavaScript is an extremely flexible programming language that many developers of all types have varying levels of expertise with. What many don't know is that you can use JavaScript on the server side as well as the client side. On the server side it can be used by installing the open source product "Node.js". Node is a powerful utility that can be used to create pure JavaScript servers as well as executing any arbitrary JavaScript code.

We can use Node to run our own shell scripts for creating jobs and utilities to help us manage our linux based servers. This also works on Macs and should work on Windows as well. To get started, you need to install Node on your system, you can start by visiting nodejs.org and following the instructions to install node on your platform.

Installing node is easy on most systems, for instance on ubuntu you will just run the commands:

sudo apt-get install nodejs
sudo apt-get install npm

Once node is installed, you can create a simple shell script easily, just make the first line of the file as follows:

#!/usr/bin/env node

In some cases, such as in Ubuntu, the binary is called nodejs instead of just plain node so you will need to change the line to:

#!/usr/bin/env nodejs

In this tutorial, we will use the extension .js, just to help us know that the script contians JavaScript code and not standard unix shell commands, but you can name the scripts ending with .sh if you wish.

The first script that we will use will be a simple script that will output the text "Hello JavaScript". You can use any file editor, personally, I use vi. Create a file named "hello.js" and use the following for its contents:

#!/usr/bin/env node
//This is my first script.

console.log('Hello JavaScript');

Two things you will notice besides the header which we previously discussed. The second line starting with "//" is a comment. Comments are ignored by the interpreter but are very useful when developing large and complex scripts. Everyone forgets what their original logic or intention was when coding a script and it's much easier to read a comment than it is to try to understand large and complex sections of code.

The next thing you will notice is console.log function. This will print text to the console, much line the unix "echo" command. console is an object, which has a function called log which takes a text string as an argument.

Assuming you are running on unix, before we can run this script, we must first make it executable. To do this we will use the unix chmod command:

chmod u+x hello.js

Now our script is executable. This command basically tells unix to set the x (executable) flag for the user level access of the file. Now we are able to run the file. If you don't have "." in your unix PATH environment variable, then you will need to proceed the name of the script with "./" to execute it. It is generally considered to be a security risk to put "." in your PATH evironment variable, so we will assume that you don't have it. Now you can execute your script by using the following command:

./hello.js

You will see the text "Hello JavaScript" output to the console, congratulations, you have created your first JavaScript shell script! Now you can move on to the next topic where we will discuss using variables in JavaScript.

Prev (TOC) | Next (Variables)


Blog Entries
Blob Entry 1
Blob Entry 2
Blob Entry 3
Blob Entry 4
Blob Entry 5
Blob Entry 6