# Day 5 - Shell Scripting — Fundamentals for DevOps & Linux Admins

## 🧠 **What is Shell Scripting?**

**Shell scripting** is nothing but **automation** — the process of converting **manual repetitive tasks** into **automatic commands**.

> 💡 In simple words:  
> Shell scripting reduces manual effort by executing predefined Linux commands automatically.

---

## ⚙️ **Fundamentals of Shell Scripting**

### Why do we need Shell Scripting?

In Windows, we can create files/folders easily using the GUI.  
In Linux servers (especially in cloud and DevOps), there’s no GUI — everything is done through **command line**.

A **shell** allows you to talk to your **operating system** using **commands**.

---

## 🧩 **What is a Shell?**

A **Shell** is a **command-line interpreter** that acts as a **bridge between the user and the Linux kernel**.  
When you type a command, the shell interprets it and sends it to the OS for execution.

---

## 🧮 **Different Types of Shells**

| Shell | Description |
| --- | --- |
| `sh` | Bourne Shell (original UNIX shell) |
| `bash` | Bourne Again Shell (most popular and default in Linux) |
| `ksh` | Korn Shell |
| `csh` | C Shell |
| `dash` | Debian Almquist Shell (used in Ubuntu for `/bin/sh`) |

👉 We’ll learn using **bash**, since it’s widely used for scripting and DevOps.

---

## 🧰 **Purpose of Shell Scripting in DevOps**

A DevOps Engineer performs automation and system management tasks, such as:

* Infrastructure management
    
* Code management (Git repositories)
    
* Configuration management (Ansible, Puppet, etc.)
    
* Monitoring system health
    
* Scheduling jobs (cron)
    

### Example:

Imagine a DevOps Engineer at Amazon managing **10,000 Linux VMs**.  
They need to monitor:

* CPU utilization
    
* Memory usage
    
* Disk space
    

Doing this manually on each VM is impossible.  
Instead, a **shell script** can:

* Log in to each VM
    
* Collect health data
    
* Send an **email alert** if usage crosses limits
    

✅ **Result:** Fully automated health monitoring.

---

## ✍️ **How to Write a Shell Script**

1. **Create a file**
    
    ```plaintext
    vi testscript.sh
    ```
    
2. **Add the shebang**
    
    ```plaintext
    #!/bin/bash
    ```
    
3. **Write commands**
    
    ```plaintext
    echo "My name is Dinesh"
    ```
    
4. **Make it executable**
    
    ```plaintext
    chmod +x testscript.sh
    ```
    
5. **Run the script**
    
    ```plaintext
    ./testscript.sh
    ```
    
    OR
    
    ```plaintext
    sh testscript.sh
    ```
    

---

## 🔹 **Shebang (**`#!`) Explained

The **shebang line** tells Linux which shell interpreter to use to run the script.

Examples:

```plaintext
#!/bin/bash     # Use Bash shell
#!/bin/sh       # Use default shell (might point to bash or dash)
#!/bin/ksh      # Use Korn shell
```

### ⚠️ Important Note:

* Earlier, `/bin/sh` was linked to `/bin/bash`.
    
* In newer Ubuntu/Debian systems, `/bin/sh` points to `/bin/dash`.  
    Dash is faster but lacks many bash features.
    

✅ So, always use:

```plaintext
#!/bin/bash
```

---

## 💬 **Comments in Scripts**

Use comments (`#`) to describe your script.  
It helps you and others understand the purpose.

Example:

```plaintext
#!/bin/bash
# create a folder
mkdir folder1

# create two files
touch file1 file2
```

---

## 🧾 **Metadata (Author Information)**

Always include a header for versioning and documentation.

```plaintext
#!/bin/bash
#########################################################
# Author: Dinesh
# Date: 10-10-2025
# Version: v1
# Description: This script outputs the node health
#########################################################
```

---

## 🖨️ **Displaying Output using** `echo`

```plaintext
#!/bin/bash
#########################################################
# Author: Dinesh
# Script to display node health
#########################################################

echo "Disk Usage:"
df -h

echo "Memory Usage:"
free -g

echo "CPU Count:"
nproc
```

---

## 🐞 **Display Output using Debug Mode (**`set -x`)

For long scripts (hundreds of lines), using `echo` for every step isn’t practical.  
Enable debug mode to print each command before execution:

```plaintext
#!/bin/bash
set -x
df -h
free -g
nproc
```

Disable by commenting:

```plaintext
#set -x
```

---

## ⚠️ **Error Handling**

### 1\. `set -e`

Stops the script immediately if any command fails.  
This prevents executing the next steps if an earlier one fails.

### 2\. `set -o pipefail`

Ensures that errors in piped commands (`|`) are also caught.

### Example:

```plaintext
#!/bin/bash
set -x
set -e
set -o pipefail

df -h
free -g
nproc

ps -ef | grep "ssh" | awk '{print $2}'
```

---

## 🔍 **Process and Column Filtering**

### Find process:

```plaintext
ps -ef | grep "ssh"
```

### Extract specific column:

```plaintext
ps -ef | grep "ssh" | awk '{print $2}'
```

---

## 🧾 **DevOps Use Case — Searching for Errors in Log Files**

When an application fails, logs are the first thing to check.  
But logs are often huge and stored in remote storage (e.g., AWS S3, Azure Blob).

### Common commands:

```plaintext
# Download and search logs directly
curl <logfile_url> | grep "ERROR"

# Or download log file
wget <logfile_url>

# Then search locally
cat apache.log | grep "ERROR"
```

---

## 🔍 **find Command Example**

To locate files anywhere in Linux:

```plaintext
find / -iname apache.log
```

---

## 🔁 **Loops in Shell Scripting**

### 🔸 `if-else` Syntax

```plaintext
if [ expression ]
then
    # Commands to execute if condition is true
else
    # Commands to execute if condition is false
fi
```

### Example:

```plaintext
a=4
b=10

if [ $a -gt $b ]
then
    echo "a is greater than b"
else
    echo "b is greater than a"
fi
```

---

### 🔸 `for` Loop

Used when we want to **repeat actions multiple times**.

Example — print numbers from 1 to 10:

```plaintext
for i in {1..10}
do
    echo "Number: $i"
done
```

Another example — list all files in a folder:

```plaintext
for file in /var/log/*.log
do
    echo "Checking $file"
    grep "ERROR" $file
done
```

---

## ✅ **Summary**

| Concept | Description |
| --- | --- |
| **Shell** | Interface between user and Linux OS |
| **Shell Script** | File with shell commands |
| **Shebang (**`#!`) | Defines which shell to use |
| `set -x` | Debug mode |
| `set -e` | Exit on error |
| `set -o pipefail` | Catch errors in piped commands |
| **Use Case** | Automate monitoring, file management, log analysis |
| **Loops** | Control execution flow (`if`, `for`, etc.) |

---

Would you like me to continue the next section with **real DevOps examples**, like:

* Script to **monitor CPU/memory usage** and send email alerts
    
* Script to **rotate and compress logs daily**
    
* Script to **check service status across multiple servers**
    

Those will make your notes *production-ready* 🚀
