Basic Linux Shell Scripting for DevOps Beginners

Basic Linux Shell Scripting for DevOps Beginners

#90 Days of DevOps Challenge - Day 4

Table of contents

No heading

No headings in the article.

▶Introduction to the Bash Shell

The Linux command line is provided by a program called the Shell. The default shell for many Linux distros is the GNU Bourne-Again Shell (bash). Bash is succeeded by Bourne Shell (sh).

When you first launch the shell, it uses a startup script located in the .bashrc or bash_profile file which allows you to customize the behaviour of the shell.

When a shell is used interactively, it displays a $ when it is waiting for a command from the user. This is called the shell prompt.

[user@vm-tests2 devops-zero-hero]$

If the shell is running as root, the prompt is changed to #. The superuser shell prompt looks like this:

[root@vm-tests2 ~]#

Bash is very powerful as it can simplify certain operations that are hard to accomplish efficiently with a GUI.

▶What is a Bash Script? A file extension of .sh.

By naming conventions, bash scripts end with a .sh. However, bash scripts can run perfectly fine without the sh extension.

Scripts are also identified with a shebang. Shebang is a combination of bash #and bang ! followed the bash shell path. This is the first line of the script. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

#!/bin/bash

▶Execution rights

Scripts have execution rights for the user executing them.

An execution right is represented by x. In the example below, my user has the rwx (read, write, execute) rights for the file testscript.sh

▶File colour

Executable scripts appear in a different colour from the rest of the files and folders.

In my case, the scripts with execution rights appear in green.

▶How to Create Your First Bash Script

Create a file named test-script.sh inside any directory of your choice using the command touch.

touch test-script.sh

Write the command.

echo "I will complete #90DaysOofDevOps challenge"

echo "End of my shell script"

To the console and your script will look something like this:

#!/bin/bash

echo "I will complete #90DaysOofDevOps challenge"

echo "End of my shell script"

Provide execution rights to the user.

Modify the file permissions and allow execution of the script by using the command below:

chmod u+x test-script.sh

chmod modifies the existing rights of a file for a particular user.

Run the script.

You can run the script in the following ways:

./test-script.sh

bash test-script.sh

This is the output:

▶The Basic Syntax of Bash Scripting

Bash scripting follows a set of rules to create programs understandable by the computer.

▶How to define variables

We can define a variable by using the syntax variable_name=value. To get the value of the variable, add $ before the variable.

Variables store data in the form of characters and numbers.

For example, the following creates a shell variable and then prints it:

To prompt the user with a custom message, use the -p flag.

read -p

You can develop advanced scripts which contain conditional statements, loops, and functions.

The Shell scripting will make your life easy and Linux administration an amazing task.

▶ Some other simple examples of Shell Scripting:

▶A basic summary:

. The Kernel is the nucleus of the operating systems, and it communicates between hardware and software

. Shell i*s a program which interprets user commands through CLI like Terminal*

. The Bourne shell and the C shell are the most used shells in Linux

. Linux Shell scripting is writing a series of commands for the shell to execute

. Shell variables store the value of a string or a number for the shell to read

. Shell scripting in Linux can help you create complex programs containing conditional statements, loops, and functions

. Basic Shell Scripting Commands in Linux: cat, more, less, head, tail, mkdir, cp, mv, rm, touch, grep, sort, wc, cut and, more.