Click here to Skip to main content
15,884,062 members
Articles / Web Development / Node.js

Linux's [yes] Command Cloned with node.js

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
26 Jul 2013CPOL2 min read 8.1K   3   2
Re-implementing linux's yes command using node.js

Introduction

I started learning Node.js. I consider this platform will be a widely used one and will have a great success in the near future. I know, many start-ups and larger companies have products based on Node.js but it is not as widespread between back-end technologies as .NET or Java is.

I thought, for fun, I will implement some of the most used Linux shell commands. I started with the yes command. The yes command repeatedly outputs a string until the process is killed. If you pass a parameter to this command, it will repeat that parameter as string or it will put out the letter 'y'.

Here is the code of my node.js version of yes command:

JavaScript
#!/usr/bin/env node

/*

    This small node.js app should do exactly what the yes linux command does.
    
    Quote from man yes:
        Repeatedly output a line with all specified STRING(s), or `y'.

*/

var DEFAULT_TEXT = "y\n";
var CUSTOM_TEXT = "";

var printUsage = function() {
    process.stdout.write("Usage: yes <optional_parameter>\n");
};

var writeDefaultText = function() {
    process.stdout.write(DEFAULT_TEXT);
};

var writerCustomText = function() {
      process.stdout.write(CUSTOM_TEXT);
};

//handler for CTRL + C
process.on("SIGINT", function() {
    clearInterval(writeDefaultText);
    clearInterval(writerCustomText);
    process.exit(0);
});

if(process.argv.length == 2) {
    setInterval(writeDefaultText, 2);
}
else if(process.argv.length == 3) {

    CUSTOM_TEXT = process.argv[2];
    if(CUSTOM_TEXT[CUSTOM_TEXT.length - 1] != "\n") {
        CUSTOM_TEXT += "\n";
    }
    setInterval(writerCustomText, 2);
}
else {
    printUsage();
} 

I think the code is really simple and pretty self explanatory, but let's look at the more trickier parts. The first line of the code notifies the shell, this file should be executed using the node command. Afterwards, two global variables are declared: DEFAULT_TEXT and CUSTOM_TEXT.

There are 3 functions declared, printUsage(), writeDefaultText(), writeCustomText(). All of these put out some value to the process.stdout stream.

I think the most important part of the code starts when there is an event handler attached to the process. This is done through the process.on("event", handlerFunction) method, which is part of the Node.js framework. In the code we define, when the process received a SIGINT signal, then the attached event handler function should be invoked. You can send/raise the SIGINT signal to your process with the help of CTRL+C key combination.

Afterwards the two cases are treated, if there was no parameter passed to the process, it will start to put the 'y' value and a new line, till the process receives the SIGINT signal. If there was a parameter passed to the process, it will put that out and a newline character repeatedly. In case there is more than one parameter passed to the app, it will print out a helper text, which shows how to use this mini app.

To be able to start the app, you have to change the file to be executable (if you are on Linux, just use chmod 744 fileName) and launch it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Hungary Hungary
I'm a software engineer who likes challenges, working in a team, I like learning new things and new technologies. I like to work with cutting edge technologies, I like to help out my collegues and team members. I have experience in building Desktop Applications based on .NET WPF and WinForms technologies. I used Silverlight for Web Application Development, but I'm also familiar with HTML5, CSS3, ASP.NET MVC, JavaScript and jQuery.

Recently, I work as a full time Java backend developer.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Amir Mohammad Nasrollahi26-Jul-13 20:33
professionalAmir Mohammad Nasrollahi26-Jul-13 20:33 
GeneralRe: My vote of 5 Pin
Gergo Bogdan27-Jul-13 5:22
Gergo Bogdan27-Jul-13 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.