A simple nodejs Custom CLI example using setRawMode

This will be a simple CLI Interface nodejs example that will serve as a starting point for a nodejs project. I might make a few examples based of this actually so I hope to get this at least somewhat solid before moving on to additional examples. First off the goal here is to create a custom command line interface that works like various command line tools like the nano text editor, or something to that effect. That is that I call a command, maybe pass some options, and then drop into a terminal based interface in which I need to use arrow keys to navigate, and type text, and keyboard shortcuts to move around. This might differ from other kinds of CLI interfaces that work by prompting for some input and function like a command line. This sort of CLI interface can be created by making use of the setRawMode method, but doing so is a little tricky, thus writing this post is called for.

The use of the setRawMode method will set up an interactive command line environment where I can define what all the various key inputs will do. However this will even include keystrokes like ctrl+c that are used to escape a command line interface, so when doing something like this I should take care to make use that I put in a way for the user to escape the Command Line Interface.

So it would be nice to make a custom interface so that when someone just enters the name of the command, or runs my script they enter a Command Line Interface where the arrow keys, or some combination of letter keys can be pressed to move around, and preform actions. However it would also be nice to make things work in a way in which input can be passed in by way of pipping when calling the script from the command line also. So there will need to be a raw mode of sorts that will be used when just calling the script by itself, and a not raw mode that will be used when the script is being piped some input.

When it comes to doing this sort of thing there are a number of packages to use to just get up and running with this fast, but this is not that kind of post. What I am doing here is a simple starting point that is just a little vanilla javaScript code and that is it. I like to make these kinds of scripts now and then that just work with node itself.

Read More

Linux exit bash built in command

The Linux exit command is a one of many commands built into the bash command, which at the name suggests is used to exit. The command will come up when it comes to writing bash scripts, and I want to have a way to end the process with a certain exit status code. By default the status code should be zero, but that might change in some situations, so it is generally always a good idea to give a status code when using it.

There might not be that much to write about when it comes to the exit command, for the most part I just type it in the bash script and give a status code as the first and only argument. However when it comes to status codes maybe there is a bit more to branch off with when it comes to this topic when it comes to special parameters, mainly the \$\? parameter that can be used to obtain the exit code of the lass ended process.

So then in this post I will be going over a few quick basic Linux exit command examples, and then maybe also touch base on some basic bash script examples that make use of the exit command also.

Read More

Case AKA Switch statements in Linux Bash Scripts

When it comes to writing bash scripts I might need to write a case statement or two once in a while. In just about any programing language there are if statements as a way to go about creating a conditional, however there are often switch statements also as another option when it comes to the subject of control flow. In bash script there are of course if statements, but the scripting language also supports a switch statement syntax it is just that they are often called a case instead actually sense that is the built in bash feature that needs to be used to create one.

Read More

The Linux head command

The Linux head command is a way to just print the first few lines, or the first few bytes of some output rather than the whole thing. In addition there is also the tail command that can be used as a way to print just the last few lines, or bytes of some output. In some situations this is just what I would want to do with some command output rather than make use of some other options, such as the less command, or redirection of output to a file that I can then option with a text editor like nano. However if I just want to not have everything spit out at me into the console, there are additional options that allow for me to just look over the full output. Still in this post I will be going over some examples of the head command in Linux, and also a whole bunch of other little related commands and examples that might come up in the process of doing so.

Read More

Sort lines of text in Linux with the sort command

The Linux sort command is a way to go about sorting lines of text by some kind of index value that is in each line. For example say I am using the Linux ps command to get a long list for each process running on a system, and I want that list sorted by how much memory each process is using, the Linux sort command can help me with that kind of task.

So to use the Linux sort command first I need some output that I can pipe into the Linux sort command. This output can be from a command like the ps command, ls command, and so forth. In addition it can also be some text in a file that I can open with something like the cat command and pipe to the Linux sort command. In any case the text does need to be in a Linux friendly format where each field is separated by a space, and each line is terminated with a line feed. Most Linux commands do this to begin with, however in some cases the output might have to be formated for sort first.

Read More