Click here to Skip to main content
12,815,937 members (36,021 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 26 Jul 2013

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

, 26 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Re-implementing linux's yes command using node.js


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:

#!/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() {

var writerCustomText = function() {

//handler for CTRL + C
process.on("SIGINT", function() {

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 {

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.


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


About the Author

Gergo Bogdan
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.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Amir Mohammad Nasrollahi26-Jul-13 21:33
professionalAmir Mohammad Nasrollahi26-Jul-13 21:33 
GeneralRe: My vote of 5 Pin
Gergo Bogdan27-Jul-13 6:22
memberGergo Bogdan27-Jul-13 6: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 27 Jul 2013
Article Copyright 2013 by Gergo Bogdan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid