DEV Community

Cover image for Linux Shell Scripting for DevOps: A Beginner's Guide
Arbythecoder
Arbythecoder

Posted on

Linux Shell Scripting for DevOps: A Beginner's Guide

Introduction

Hello there! Do you remember our last conversation about mastering Git? Now, it's time to delve into a more exciting part of your DevOps journey: Linux shell scripting.

Shell scripting is a powerful tool for automating tasks and managing systems, making it an essential skill for DevOps engineers. This guide provides a basic introduction to Linux shell scripting, with a focus on its applications in the DevOps field. Here, we'll cover essential topics to help you get started.

  • What is Shell Scripting?
  • Writing Your First Shell Script
  • Variables and Data Types
  • Control Flow Statements
  • Functions
  • Error Handling
  • Working with Files and Directories
  • Networking
  • DevOps Applications of Shell Scripting

What is Shell Scripting?

Shell scripting is a form of programming that allows you to automate tasks by creating scripts that the shell interprets and executes. In Linux, the most common shell for scripting is Bash, and that's what we'll focus on in this guide.

How to Write Your First Shell Script

To write your first shell script, you need a text editor. You can use popular editors like vi, Vim, or Nano. Here's how to create a simple "Hello, world!" script:

  1. Open your preferred text editor.

  2. Save a new file with the .sh extension, e.g., hello.sh.

  3. Add the following content to the file:

#!/bin/bash

echo "Hello, world!"
Enter fullscreen mode Exit fullscreen mode
  1. Save the file.

  2. Make it executable using the following command:

chmod +x hello.sh
Enter fullscreen mode Exit fullscreen mode
  1. Finally, run the script:
./hello.sh
Enter fullscreen mode Exit fullscreen mode

The script will execute and print "Hello, world!" to the console.

Variables and Data Types

Shell scripts use variables to store data. These variables can hold various data types, including integers, strings, and Booleans. To declare a variable, use the following syntax:

variable_name=value
Enter fullscreen mode Exit fullscreen mode

For example, to store the string "welcome to a new world!" in a variable named my_variable, you would write:

Image description

To access a variable's value, simply use its name with a $ prefix. For instance, to print the value of my_variable, you would use:

Image description

Control Flow Statements

Control flow statements are used to manage the order of execution in shell scripts. The most common ones are:

If Statements:

if condition
then
    code_to_execute
fi
Enter fullscreen mode Exit fullscreen mode

For example:

Image description

For Loops:

for variable_name in list
do
    code_to_execute
done
Enter fullscreen mode Exit fullscreen mode

Example:

Image description

While Loops:

while condition
do
    code_to_execute
done
Enter fullscreen mode Exit fullscreen mode

Example:

Image description

Functions

Functions allow you to group code together for improved modularity and readability. Here's how to define a function:

function function_name() {
    # Code to execute
}
Enter fullscreen mode Exit fullscreen mode

For example:

Image description

To call a function, simply use its name:

greet
Enter fullscreen mode Exit fullscreen mode

This wraps up our beginner's guide to Linux Shell Scripting for DevOps. Shell scripting is a valuable skill for anyone working in the DevOps field, and with practice and dedication, you can become proficient in automating tasks and managing systems.

Top comments (25)

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Dunno if this is a me thing, or worth mentioning in the article, but it took me quite a while to get used to not putting spaces around the = when defining variables.

Also, any plans on making a more advanced guide too?

Collapse
 
jihedkdiss profile image
Jihed Kdiss

I did quick research and apparently the preferred way to use = in bash is without spaces unlike many other languages. Even though it's not explicitly mentioned here, you can see that Google themselves have used variable=value so many times in their official code style guide.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

It's not just preferred, you actually can't put spaces around the =

Thread Thread
 
arbythecoder profile image
Arbythecoder

Thank you for the observation and thank you for reading 😊

Collapse
 
arbythecoder profile image
Arbythecoder

Well noted, thank you for reading 💪

Collapse
 
arbythecoder profile image
Arbythecoder

it's just for the article's sake but thanks for the observation, well noted .
Yes, I am working on advanced guide too. Thank you for reading 😊

Collapse
 
guinuxbr profile image
Guilherme L. Leite Marques

Nice article. It is a good idea to use fenced code blocks rather than screenshots for accessibility. Screenshots make things more difficult for people who use screen readers. Other than that, fenced code blocks are easy to copy and paste and, therefore, test the proposed code.

Collapse
 
arbythecoder profile image
Arbythecoder

Thank you so much, duly noted.

Collapse
 
jihedkdiss profile image
Jihed Kdiss

This is a good and straightforward tutorial, Good job Arby! However the title is a bit misleading since you didn't mention any DevOps specific commands.

Collapse
 
arbythecoder profile image
Arbythecoder

Thank you so much, I am glad you found it valuable 🙏

Collapse
 
muthukumark98 profile image
MuthuKumar

Awesome explanation 😸 thanks 🙏

Collapse
 
arbythecoder profile image
Arbythecoder

Thank you 👍 I'm glad you find my article valuable

Collapse
 
taijidude profile image
taijidude

Good Stuff. Thank you for sharing.

Collapse
 
icolomina profile image
Nacho Colomina Torregrosa

great !

Collapse
 
pxlmastrxd profile image
Pxlmastr

Can I ask where you download MINGW64? I just can't find where to download it.

Collapse
 
arbythecoder profile image
Arbythecoder
Collapse
 
pxlmastrxd profile image
Pxlmastr

Oh my gosh, thank you so much! You just made my day

Collapse
 
emma32 profile image
emma devid

Great article! Utilizing fenced code blocks instead of screenshots is a smart move for accessibility. Screenshots pose challenges for those using screen readers. Additionally, fenced code blocks are user-friendly, allowing for easy copying, pasting, and testing of the provided code.

Collapse
 
sc0v0ne profile image
sc0v0ne

Simple and amazing !!!

Collapse
 
lucasgrocha profile image
Lucas Rocha

I'm not a DevOps guy, but I loved this!

Collapse
 
arbythecoder profile image
Arbythecoder

I am happy to hear this! Thank you so much

Collapse
 
taqui_786 profile image
TAQUI ⭐

Nice Post

Collapse
 
rijultp profile image
Rijul Rajesh

Nice article, Straight to the point!

Collapse
 
abhixsh profile image
Abishek Haththakage

Awesome explanation!!

Collapse
 
virginiaslifer profile image
VirginiaSlifer

This next step in the DevOps journey promises to be both challenging and rewarding. Tiger exchange