# Day 4: Basic Linux Shell Scripting for DevOps Engineers

## **Shell Scripting**

Usually, shells are interactive, which means they accept commands as input from users and execute them. However, sometimes we want to execute a bunch of commands routinely, so we have to type in all commands each time in the terminal.

As a shell can also take commands as input from file, we can write these commands in a file and can execute them in shell to avoid this repetitive work. These files are called **Shell Scripts,** Each shell script is saved with `.sh` file extension e.g., [**myscript.sh**](http://myscript.sh)**.**

A shell script has syntax just like any other programming language. If you have any prior experience with any programming language like Python, C/C++ etc. It would be very easy to get started with it.

A shell script comprises the following elements –

* Shell Keywords – if, else, break etc.
    
* Shell commands – cd, ls, echo, pwd, touch etc.
    
* Functions
    
* Control flow – if..then..else, case and shell loops etc.
    

Lets have a look on our First program

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728468047974/7a0be8ff-bbb5-4993-afa5-cf78514aa6ec.png align="left")

#!” on top of the script file, followed by the absolute path of the shell of choice. To specify bash as an interpreter, Add the following line on top of the shell script.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728477334460/c8e82e30-f8fa-4874-9895-614b35dfb38e.png align="left")

## Comparison Operators

You can compare two variables in shell scripting. We do these comparisons to make decisions, we will see how to do that later in this article, but before that, here is a list of some comparison operators.

### Integer comparison

| **Operator** | **Description** |
| --- | --- |
| \-eq | is equal to |
| \-ne | is not equal to |
| \-gt | is greater than |
| \-ge | is greater than or equal to |
| \-lt | is less than |
| \-le | is less than or equal to |

### String Comparison

| **Operator** | **Description** |
| --- | --- |
| \== | is equal to |
| != | is not equal to |
| \\&lt; | is less than, in ASCII alphabetical order |
| \\&gt; | is greater than, in ASCII alphabetical order |

We add a \\ before &lt; and &gt; because they need to be escaped when typed in the \[ \] construct. Now, let’s see where these are used.

## Conditional statements

Conditional statements are used to execute a block of code only when certain conditions are met. Shell scripts support the use of conditional statements. We use comparison operators to check the conditions. Let’s see a few conditional statements.

### If statement

It checks the condition, and if it is conditioned true, it executes the commands.

**Syntax**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728478126723/40b13d1e-5507-4dab-938b-c225f47a331d.png align="left")

let’s take an example,

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728478287264/ed2999e4-1755-4bb8-b4f8-78cee0b73290.png align="left")

### If-else statement

In an if-else statement, you can specify a set of commands to run if the condition is not met.

**Syntax**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728478535268/0b12fc8c-f6ce-4771-a6ff-73967bf867e2.png align="left")

Let’s see an example

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1728478796258/9922b77c-0d7a-4046-be68-960fe72b2e51.png align="left")

the Loops and For-Loops concepts are pending, hands-on practice is important for learning shell scripts.
