[ DAY-4 ]: Basic Linux Shell Scripting for DevOps Engineers 🛠

[ DAY-4 ]: Basic Linux Shell Scripting for DevOps Engineers 🛠

·

3 min read


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.