Conditional Statements in Linux Bash scripts

In bash scripts it is possible to define conditional statements, or just simply if statements if you prefer. These statements work more or less the same as in many other languages, however the syntax is of course a little different.

if statements are ways to go about making decisions in bash scripts by having some kind of expression that will result in a true of false value. If the expression is true then additional code in the if statement will run, if not it will not. In addition there is also the option of including and else statement where some code that will only run if an expression evaluates to false.

There are also case statements that are like what is often referred to as a switch statement in other languages. These kinds of statements can also be thought of as a kind of conditional statement in bash scripts.

Read More

The _.slice method in lodash

In native javScript there is the Array slice method that will return a new array from another array without mutating the array in place. There is also yet another method in the core javaScript Array prototype object called splice that does more or less the same thing as Array slice only it will mutate the array in place. This however is a post on the slice method in lodash that is not just a reference to the native Array slice method.

The Array slice method in plain old javaScript by itself will work more or less just as well as the lodash slice method, but there is at least one note worthy thing that is different.

Read More

Linux Bashrc file command aliases

In the home folder of most Linux systems that use bash as the command shell there should be a hidden file called .bashrc. This file will be called each time I start a new terminal window, so it is a good place to do things like set what the format of the bash command prompt should be. However there are many other things that I can do with the script, and one such thing that is pretty helpful is setting up some bash aliases for commands.

Often there might be a command that I use with a bunch of options that take a long time to type over again. There are also a number of situations in which I can produce a result that I want, but not with a single command, rather a long string of commands involving piping of standard output to the standard input of another. So with many of these it makes sense to create a line in a file where I assign a command with a long string of options, and or a bunch of commands piped together that will result in just one command with a short name. I can then just call that single short command name each time, rather than typing the same long string of text each time.

I can also make it so an alias is a call to a bash script, and from there I can use positional arguments, look at environment variables, and so forth in order to make the thing look and work like a real Linux command.

Read More

Linux Bash Scripts

In a Linux system it pays to know a thing or two about bash scripts. A bash script is a way to take a bunch of commands and place them in a text file, the text file can then be called with the bash command, or be made executable by adding a bash shebang at the top of the file, and then using the chmod command to set the permissions of the file so that it is executable, thus the script can be called directly.

In any case bash scripts are a way to take a task consisting of one or more commands that one might find themselves repeating often and pull the string of commands into a file that can then be called just once. There are then ways to edit various configuration files to make it so that the bash scripts can be called from a terminal window with just a single command name. Although it might be outside the scope of this post there is also the idea of making the script run each time a system starts, or to act as some kind of service.

Bash is not the most capable language when it comes to programing, but it is a way to interact with Linux commands including commands such as node which allows for running javaScript code. There is also writing ones own commands with node, or any other preferred programing environment and using bash as a way to tied things together.

In this post I will be going over the very basics of bash scripts though, and will be linking off into other topics as needed. So this is a getting started post, but also a general index post on all things bash script related that I have written about thus far.

Read More

A percent module javaScript example

I think it might be a good idea to work out some more basic javaScript examples that might lead to useful modules that I might use in an actual project or two. One thing that seems to come up a lot for me when working on projects is dealing with what I would often call a percent value, or a value that can often be expressed as a percent value. That is having a method that will return a number between 0 and 100, or between 0 and 1 which could be multiplied by 100 or any other number for that matter.

There is having a simple percent method that will take two arguments one would be a numerator and another a denominator and the returned result will be a number between 0 and 1. So this can be thought of as a kind of normalization in the sense that any two set of numbers will be a value between 0 and 1 so that they can be compared to each other. However there are all kinds of ways that one could go about making such a function other than just divining the numerator over the denominator.

There could also be some additional code that has to do with clamping or wrapping when this go out of range, but a basic example of this kind of method is not to hard. Still just a basic example of this kind of method will just give numbers that will go up in a very linear or straight line kind of way. So there is a wide range of other kinds of methods such as this that could give percent values that follow a curve, or some other kind of pattern.

These kinds of methods come into play when it comes to writing logic that has to do with animations, but have a wide rang of other uses such as when creating an experience point system for example.

Read More