Introduction to Bash Scripting

When I first started working with servers and the command-line interface (CLI), I was fascinated to see the amount of power that a system admin had in all GNU/UNIX computers (i.e. a MacBook or a Ubuntu Server). With the simple declaration of sudo at the beginning of any command, you have the ability to run the command as the root user! This user has the highest privilege levels and can do pretty much everything you wish, including deleting the system folders.

I have recently been attempting to create some scripts to automate the monitoring of a log file and create an alert for a specific event to patch a bug affecting one of my projects. While I can tail this log and observe the verbose generated, this is not sustainable when I have to monitor the logs for hours on end. Sadly not even the mighty sudo command could help me here.

To resolve this issue I opted to create a script that monitors this log file, create a new file that will store the errors logged, and write the error to the target file. Now, there are many approaches to resolve this issue, including the creation of a Python script, however since I will be working with CLI commands, I opted to go with the creation of a Bash Script.

WHAT IS BASH?

Bourne Again Shell (Bash) is a command processor where the user types command to complete a specific action. Bash can also read and execute commands from a file, called a shell script.

This is the best option to create the monitoring script as the shell script allows me to run UNIX commands such as tail, grep, etc, and I can use variables, functions, and comments to program the script to complete my desired tasks.

HOW DOES IT WORK?

You will find that coding a bash script is fairly similar to more mainstream programming languages such as Java. The truth is that scripting in bash is not very difficult if you have some programming experience, but I will be splitting this tutorial into several parts to make sure I am as thorough as possible. Let’s begin by checking the components that can have a bash script:

First step

When you’re creating a bash script, the first step that you have to take is to declare the path where bash is located in your server. By default in all Linux and UNIX systems, this is located under /bin/bash. To declare this path and start coding all the bash scripts you will need to add this path to the first line of the file as follows:

#! /bin/bash

Comments

The next part of the script is commenting. To comment in a bash or shell script you just need to add a # sign at the beginning of the line:

# This is a comment

Variables

By convention, the variables in a bash script are written in uppercase letters. Also, the variables can be composed of letters, numbers, and underscores:

NAME=”Isaac”
BIRTH_DATE=”Apr-21″
NUM1=30
AGE=50

When you first declare the variable you just have to write just the name of the variable in uppercase letters, but to invoke the variable later in the code you have two options:

  • Use $NAME
  • Or $(NAME) similar to how you’d do if you were programming in JavaScript

User inputs

You can also allow the user to input data straight from the CLI by using the read command. With this command you can use the -p flag to enter a label text that can be used to instruct the user into taking some action:

read -p “Enter your name: ” NAME

Running system commands

Now for the final part of this post, we will check how to run system commands such as echo, tail, mv, etc. in a script. This is where a bash script shines as you can simply write the command in a new line and even apply some logic to it (loops, validations, etc.) which we will cover in my next post:

# ECHO COMMAND
echo Hello World!

# Now let’s call the NAME variable from above inside an ECHO command
echo “Hello $NAME, nice to meet you!”

CONCLUSION

Now let’s put all that we’ve learned so far in a single script that asks the user for his/her name and then greet the user:

#! /bin/bash
# DECLARING VARIABLES NEEDED
NAME=”NO_NAME”
# ASKING USER FOR INPUT
read -p “Enter your name: ” NAME
# GREETING THE USER
echo “Hi $NAME, nice to meet you!”

You can use any text editor to create the script, but you can also create it directly from the terminal by following these steps:

  • Create the script document
    • touch myScript.sh
    • Make sure that the document name ends with .sh to indicate the operating system that this is a bash/shell script file
  • Edit the document and paste in the code from above
    • vi myScript.sh
    • Save the changes by hitting the escape key (esc) and then writing :wq
      • This will indicate VI (VIM Editor) to write the changes and quit the file
  • Change the permissions of the script file to make it executable
    • chmod +x myScript.sh
  • Run the script and see the magic!
    • ./myScript.sh

I hope you’ve liked this post and that this serves as an entry point in your Bash Scripting journey and that it serves also as a cheat sheet for when you proceed to code your first script! I will be updating this post to link part II once it’s out.

If you’ve liked this post, then share it! Also, remember to subscribe to my newsletter to be notified of new posts, updates, and offers by adding your email to the box below.

See you later and thanks for reading!

Subscribe to my Newsletter

Exclusive offers, Industry insights and more!

Join 2,031 other subscribers