Skip to main content

Command Palette

Search for a command to run...

Day 5 - Shell Scripting โ€” Fundamentals for DevOps & Linux Admins

Updated
โ€ข5 min read

๐Ÿง  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

ShellDescription
shBourne Shell (original UNIX shell)
bashBourne Again Shell (most popular and default in Linux)
kshKorn Shell
cshC Shell
dashDebian 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

     vi testscript.sh
    
  2. Add the shebang

     #!/bin/bash
    
  3. Write commands

     echo "My name is Dinesh"
    
  4. Make it executable

     chmod +x testscript.sh
    
  5. Run the script

     ./testscript.sh
    

    OR

     sh testscript.sh
    

๐Ÿ”น Shebang (#!) Explained

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

Examples:

#!/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:

#!/bin/bash

๐Ÿ’ฌ Comments in Scripts

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

Example:

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

# create two files
touch file1 file2

๐Ÿงพ Metadata (Author Information)

Always include a header for versioning and documentation.

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

๐Ÿ–จ๏ธ Displaying Output using echo

#!/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:

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

Disable by commenting:

#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:

#!/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:

ps -ef | grep "ssh"

Extract specific column:

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:

# 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:

find / -iname apache.log

๐Ÿ” Loops in Shell Scripting

๐Ÿ”ธ if-else Syntax

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

Example:

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:

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

Another example โ€” list all files in a folder:

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

โœ… Summary

ConceptDescription
ShellInterface between user and Linux OS
Shell ScriptFile with shell commands
Shebang (#!)Defines which shell to use
set -xDebug mode
set -eExit on error
set -o pipefailCatch errors in piped commands
Use CaseAutomate monitoring, file management, log analysis
LoopsControl 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 ๐Ÿš€

More from this blog

Dinesh's Blog

104 posts