Skip to main content

Command Palette

Search for a command to run...

[ DAY-4 ]: Basic Linux Shell Scripting for DevOps Engineers ๐Ÿ› 

Updated
โ€ข3 min readโ€ขView as Markdown
[ DAY-4 ]: Basic Linux Shell Scripting for DevOps Engineers ๐Ÿ› 

Introduction ๐Ÿ“š

Welcome to Day 4 of the thrilling #90DaysOfDevOps challenge! ๐ŸŒŸ Today, we're diving into the nitty-gritty of Linux shell scripting and understanding why it's a game-changer for DevOps engineers. Imagine having the prowess to automate mundane tasks, fine-tune system configurations, and elevate the entire DevOps ecosystem. That's the magic of shell scripting. ๐Ÿ’ป๐Ÿš€ So gear up! Let's embark on this enlightening journey and unravel the core tenets of shell scripting. ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ“


๐Ÿ–‹ What is Shell Scripting?

Shell scripting is the art of curating and running a command series within a shell - the command-line interpreter. It's our go-to toolkit for task automation, system administration, or sequential operations. Essentially, the shell acts as the mediator between the user and the OS, facilitating interaction with the computer's intricate resources via straightforward commands.


๐Ÿค What is Shell Scripting for DevOps?

For DevOps, shell scripting is synonymous with automation. It's all about crafting scripts that actuate various DevOps tasks and processes. At its heart, DevOps is about fostering unparalleled collaboration between software development and IT ops, ensuring swift and foolproof application and service delivery.

For the tech aficionados, in Unix-like OS (think Linux or macOS), the Bourne shell (sh), Bourne-Again Shell (bash), Korn Shell (ksh), and Z Shell (zsh) reign supreme. Meanwhile, Windows enthusiasts would be more accustomed to the Command Prompt (cmd.exe).


๐Ÿš€ What are the Benefits of Shell Scripting in DevOps?

Shell scripting isn't just codeโ€”it's the bedrock of DevOps, with a myriad of benefits:

  • ๐Ÿ” Automation: Shell scripts morph tedious manual processes into swift, automated ones, reducing human-induced errors.

  • ๐Ÿ— Infrastructure as Code (IaC): With shell scripts, infrastructure configurations become definable and manageableโ€”streamlining provisioning in both cloud and on-premises landscapes.

  • ๐Ÿ”Œ Easy Integration: DevOps tools, be it version control systems or CI/CD pipelines, effortlessly intertwine with shell scripts.

  • โœ‚๏ธ Customization: Every DevOps ecosystem is unique, necessitating bespoke scripts that align perfectly with specific needs.

  • ๐ŸŒ Portability: Platform-agnostic nature ensures scripts run seamlessly across multiple OS and distributionsโ€”an asset for diverse environments.


โ“ What is #!/bin/bash? Can we use #!/bin/sh instead?

The quirky line #!/bin/bash has an official titleโ€”it's called "shebang" or "hashbang". In Unix realms (like Linux or macOS), this is the beacon that signals the OS to deploy bash as the command interpreter.

Start a script with #!/bin/bash, and you're essentially instructing the system to engage the Bash shell for command execution. Now, if you're wondering about #!/bin/sh, it's an alternative to declare the Bourne shell as the script interpreterโ€”a simpler counterpart to Bash but universally accessible in Unix-like systems.


Create Your First Script and Execute it

Certainly! Here's a simple shell script that compares two numbers using if-else:

#!/bin/bash

# Read two numbers from the user
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2

# Compare the numbers
if [ $num1 -gt $num2 ]; then
    echo "$num1 is greater than $num2"
elif [ $num1 -lt $num2 ]; then
    echo "$num1 is less than $num2"
else
    echo "$num1 is equal to $num2"
fi

To run the script:

  1. Save the script to a file, for example, compare_numbers.sh.

  2. Make the script executable using the command chmod +x compare_numbers.sh.

  3. Run the script using ./compare_numbers.sh.

The script will prompt you to enter two numbers and then it will compare them and print out the result.