Catching up in BASH Scripting

Hey there techies, I hope things have been going well for you this last week. I apologize for not keeping to my schedule that I said I would earlier. Getting my personal work routine established and changed has been hard. Last week is when I put in my two weeks’ notice at my second job. Having been scheduled to work last weekend usually I end up finishing the job and I’m very exhausted physically and mentally. So pushing myself with two jobs to work and an additional amount of time for writing is a little difficult. Now though I seem to be getting my footing back again. So let’s continue where we left off and talk about scripting with Bash.

Resuming with BASH

As an aside for a little history, many times in the IT world your average person or even a system administrator will use some flavor of Linux. Two things to clear out of the way is that Linux is actually a kernel written in the 1970’s by Linus Torvalds. It had its inspiration from the Berkley Software system that designed UNIX. In fact, it’s quite common for the two to get confused and associated sometimes as being the same.

The Linux kernel is a very customizable platform it has to this day become one of the most diverse ecosystems for users, modders, admins, and many more. Some very common distros you might see of Linux might be Ubuntu, Debian, and Arch. Many different variations have been built on some of these foundational programs.

The reason I mention all of this, in the beginning, is that a lot of different distros of Linux come with a terminal called the Bash terminal. Bash stands for the Borne Again Shell and it’s gained a reputation for helping new computer users get used to tweaking and customizing their systems. Depending on the need and scenario, one of the strong cases for using a terminal in a person’s workflow is to use it for what’s called scripting.

Harnessing BASH

An example of using BASH and the terminal is in a business scenario where we will need to automate some processes. Say for example you work for a small startup that only has an employee pool of 10-30 people. About half of them use a computer system, and the company maintains a database system that needs to stay up to date. As a system administrator, your responsibility is the upkeep of this database.

Many times Ubuntu is a server system that is harnessed for business enterprises. It comes shipped standard with the BASH terminal. In our database example if you log into the system it can be very tedious to manually type commands one after another to do a basic system update or to use basic tools that come installed.

Now imagine having to do that across all the machines that the employees use, scenes like this can quickly evolve into a huge time sinkhole. This is where harnessing the computer’s strength for repetition works for our admin. Using a process called scripting, he can automate a repetitive task and get as many machines up-to-date and secure as possible. Let’s take a look at an example of this in action.

Basics of Scripting – Ubuntu and Arch

For our purposes today we are going to be using Ubuntu as an example of how to do scripting. The terminal I have on this machine is a modified terminal emulator that is called FISH. It comes loaded with extra features that I like to use and can’t live without. However, I will also post an example of this same process used on an Arch Linux machine.

Our procedure for doing this will be as follows we are going to write two scripts, on two different machines and they will both update and install packages for each system. After the installation, we will have the script output a confirmation that the system has been updated. After tackling this we will do the same steps next week but instead be using PowerShell on Windows. Let’s begin:

  1. In a VM, Laptop, or other platform launch an instance of Ubuntu
  2. Once a terminal is started, input vim script.bash this should open up an instance of vim and now we can get started with writing our script.

The first important part we need to focus on is what’s called the “shebang” which in this case is a declarative sequence of statements in our file that tells Linux which interpreter to parse for our file. Some general examples you might run into are /usr/bin/bash or /bin/bash. For our example here we will be using the shebang to my Ubuntu subsystem on Windows.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *