DEV Community

Paul Anaekwe
Paul Anaekwe

Posted on

The DevOps Superpower That Is Shell Scripting

Ever wondered how DevOps engineers seem to whip up automated magic? How they deploy applications with a single command, or get their servers to sing in perfect harmony? Often, the secret sauce is something surprisingly simple yet incredibly powerful: Shell Scripting.

Today, we're diving into the wonderful world of shell scripting, specifically how it empowers DevOps practitioners to automate, orchestrate, and generally make their lives a whole lot easier. Think of it as teaching your computer to do your bidding, all while you kick back and enjoy a well deserved coffee.

What in the DevOps is Shell Scripting Anyway?

In my own words, shell scripting for DevOps is like giving your computer a personalized set of instructions, written in its native language, to perform a series of tasks automatically. Imagine you're a conductor for a choir, and your computer is the orchestra. Instead of shouting out "Play the violins! Now the trumpets! Now everyone at once!", you write down a musical score that dictates every move.

Why is this a big deal for DevOps? Well, DevOps is all about speed, efficiency, and reducing manual errors. And what's more efficient than having a machine do repetitive, error prone tasks for you?

Examples in the Wild:

Automating Deployments: Instead of manually logging into servers, copying files, and restarting services, a shell script can handle the entire deployment process from start to finish. It's like having a tiny, super-fast robot do your grunt work.

Setting up Development Environments: Need to get a new developer up and running? A script can install all necessary software, configure settings, and even download project dependencies. No more "did you remember to install Node.js?" questions!

Monitoring and Alerting: Scripts can regularly check the health of your applications and servers. If something goes wrong, they can send you an alert maybe even a polite little email saying, "Hey, your server is feeling a bit under the weather."

Backup and Recovery: Automate your data backups with scripts that run at scheduled intervals, ensuring your precious information is always safe. Because losing data is about as fun as stepping on a Lego.

The Mystery of #!/bin/bash and #!/bin/sh

You've probably seen these lines at the very beginning of shell scripts. They look a bit like secret codes, don't they?

#!/bin/bash: This is called a "shebang" (yes, that's really what it's called, not a typo!). It tells your operating system, "Hey, this script needs to be executed using the bash interpreter!" Bash is a powerful and popular shell program that understands a wide range of commands and scripting features. Think of it as the ultimate language for talking to your Linux/Unix system.

#!/bin/sh: This is another shebang, but it points to the sh interpreter. sh is often a symbolic link to a more basic shell, like dash or bash itself, but running in a more POSIX compliant (standardized) mode. This means it might not support all the fancy features that bash offers.

Can we write #!/bin/sh as well? Absolutely! You can use #!/bin/sh if you want your script to be more portable and compatible across different Unix like systems, as sh aims for a common set of features. However, if you're leveraging advanced Bash specific features (like associative arrays or more complex pattern matching), then #!/bin/bash is your go to. It's like choosing between a classic, reliable car (sh) and a souped up, feature rich sports car (bash). Both get you from A to B, but one has more bells and whistles!

Let's Write Our First Script!: Shell Script Challenge

Let's write a simple script that prints a message. Open your favorite text editor, type the following, and save it as devops_challenge.sh:

Now, to make it executable:

Bash

chmod +x devops_challenge.sh

And to run it:

Bash

./devops_challenge.sh

Output:

I will complete #90DaysOfDevOps challenge.

Boom! Your first shell script. Feeling powerful yet? You should be!

Lets Get A Bit Chatty: User Input and Arguments

Shell scripts can also interact with you and take information from their environment.

Let's create a script called interactive_script.sh:

Now, let's run it. First, just with user input:

chmod +x interactive_script.sh
./interactive_script.sh

It will prompt you for your name. Enter "DevOps Enthusiast" (or whatever you like!).

Example Output (user input part):

Hello there! What's your name?
DevOps Enthusiast
Nice to meet you, DevOps Enthusiast!
Now, let's see some arguments.
The first argument is:

The second argument is:

All arguments are:

The number of arguments is: 0
Script finished. Hope you learned something cool, DevOps Enthusiast!

Now, let's run it with some arguments:

./interactive_script.sh "Shell Scripting" "Rocks!"

Example Output (with arguments):

Hello there! What's your name?
DevOps Ninja
Nice to meet you, DevOps Ninja!
Now, let's see some arguments.
The first argument is: Shell Scripting
The second argument is: Rocks!
All arguments are: Shell Scripting Rocks!
The number of arguments is: 2
Script finished. Hope you learned something cool, DevOps Ninja!

See how easy it is to get information into your scripts? It's like they're having a conversation with you!

The Art of Decision-Making: If-Else Statements

Just like humans, scripts need to make decisions. "If this happens, do that. Else, do something else." This is where if else statements come in, and they're fundamental to any meaningful script.

Let's compare two numbers, with a subtle joke or two. Create a file called compare_numbers.sh:

Now, let's run it with different numbers:

Scenario 1: First number is greater

*chmod +x compare_numbers.sh
./compare_numbers.sh 10 5 *

Output:

10 is greater than 5. Looks like 10 spent more time at the gym!
Comparison complete! Thanks for playing.

Scenario 2: Second number is greater

*./compare_numbers.sh 7 15 *

Output:

15 is greater than 7. 7 is probably just playing it cool.
Comparison complete! Thanks for playing.

Scenario 3: Numbers are equal

*./compare_numbers.sh 8 8 *

Output:

8 and 8 are equal. They must be twins, or perhaps they're just perfectly balanced, as all things should be.
Comparison complete! Thanks for playing.

Scenario 4: Missing arguments

./compare_numbers.sh 10

Output:

Oops! I need two numbers to compare, my friend. Like, really, truly two numbers.
Usage: ./compare_numbers.sh

See how the if-else (and elif for "else if") allows your script to adapt and respond differently based on conditions? It's the brains of the operation!

Conclusion

Shell scripting is an invaluable skill for any aspiring or seasoned DevOps professional. It's the glue that holds automation together, allowing you to streamline workflows, reduce errors, and reclaim your precious time. From simple tasks to complex orchestrations, shell scripts are your loyal, tireless assistants.

So, go forth, embrace the shebang, and start scripting! Your future automated self will thank you for it. And who knows, maybe one day your scripts will be so smart, they'll write blog posts about you! (Just kidding... mostly.)

Top comments (0)