DEV Community

Cover image for My road to learn devOps, Part 1
Bruno Guimarães
Bruno Guimarães

Posted on • Updated on

My road to learn devOps, Part 1

As someone who is new to the world of DevOps, I'm excited to dive deep into the intricacies of Linux commands and shell scripting. It's clear that they play an essential role in my journey towards becoming a proficient DevOps professional. This comprehensive guide on Linux commands and shell scripting will be my initial stepping stone.

I understand that mastering Linux commands and shell scripting is a necessity for me. These skills will empower me to automate tasks, manage systems effectively, and solve complex problems with ease. Although this guide provides an extensive overview, it only scratches the surface of these vast topics. Hence, I'll make sure to explore more resources available online, such as in-depth tutorials and documentation.


Linux Commands

Linux commands are the essence of any Linux environment. They allow us to interact with the system and perform a myriad of tasks. Here are some fundamental commands that every aspiring DevOps professional must know:

  • ls: This command is used to list all the files and directories in the current working directory. E.g., ls

  • cd: This command is used to change the current directory. E.g., cd /home/user/Documents

  • pwd: This command prints the current working directory. E.g., pwd

  • cat: This command is used to display the contents of a file. E.g., cat example.txt

  • touch: This command creates an empty file. E.g., touch newfile.txt

  • rm: This command removes files or directories. E.g., rm example.txt

  • cp: This command copies files or directories. E.g., cp source.txt destination.txt

  • mv: This command moves files or directories. It's also used to rename files. E.g., mv oldname.txt newname.txt

  • find: This command searches for files or directories based on given criteria. E.g., find / -name example.txt

  • grep: This command searches for a text pattern within files. E.g., grep "example" file.txt

These are basic commands. To work efficiently as a DevOps professional, you need to master many more commands related to system management, process management, networking, and more.


Shell Scripting

Shell scripting is writing a series of command for the shell to execute. It can combine lengthy and repetitive sequences of commands into a single script, which can be stored and executed anytime. This reduces the effort required by the end-user.


Basics of Shell Scripting

A shell script begins with a "shebang" (#!) followed by the path to the shell that will interpret the script. Most often, this will be /bin/bash. Here's an example of a basic shell script:

#!/bin/bash
echo "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

In this script, echo is a command that outputs the strings it is being passed as arguments. So when this script is run, it will display "Hello, World!".

You can execute a shell script by giving it the appropriate permissions using the chmod command and then running it. For example:

chmod +x script.sh
./script.sh
Enter fullscreen mode Exit fullscreen mode

The first command gives the script execution permissions, and the second runs the script. The output will be: Hello, World!


Variables in Shell Scripting

Variables can be declared in shell scripts to store information. Here's an example:

#!/bin/bash
name="John Doe"
echo "Hello, $name"
Enter fullscreen mode Exit fullscreen mode

In this script, name is a variable that holds the string "John Doe". When the script is run, it will display "Hello, John Doe".


Conditional Statements

in Shell Scripting

Shell scripts can also contain conditional statements. Here's an example:

#!/bin/bash
read -p "Enter your name: " name
if [ "$name" == "John Doe" ]; then
    echo "Hello, John Doe."
else
    echo "You are not John Doe!"
fi
Enter fullscreen mode Exit fullscreen mode

In this script, the read command takes user input. If the entered name is "John Doe", the script will display "Hello, John Doe.". Otherwise, it will display "You are not John Doe!".


Loops in Shell Scripting

Loops can be used in shell scripts to perform repetitive tasks. Here's an example:

#!/bin/bash
for ((i=1; i<=5; i++)); do
    echo "Iteration: $i"
done
Enter fullscreen mode Exit fullscreen mode

This script will display "Iteration: 1", "Iteration: 2", ..., "Iteration: 5".


Shell Scripting in DevOps

Shell scripting is crucial in the DevOps field as it automates the routine tasks. It’s used for automating the system maintenance tasks, network monitoring, updating systems in batches, data backup, and more.

Here is an example of a shell script that would check the disk usage and send an email to the system admin if it's over 90%:

#!/bin/bash
MAX_USAGE=90
EMAIL="admin@example.com"
USAGE=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
if [ $USAGE -gt $MAX_USAGE ]; then
    echo "Running out of disk space" | mail -s "Disk Space Alert" $EMAIL
fi
Enter fullscreen mode Exit fullscreen mode

In this script, df is a command that reports the system's disk space usage, awk is a command-line tool for manipulating text, and sed is a stream editor for filtering and transforming text. mail is a command for sending emails.


As I venture into the world of DevOps, I'm conscious that I will also need to grasp concepts like Continuous Integration/Continuous Deployment (CI/CD), infrastructure as code (IaC), virtualization, and containerization. Additionally, I should familiarize myself with tools like Docker, Jenkins, Kubernetes, and Ansible, among others.

I'm excited to embark on this journey and am eager to learn. I'm determined to keep expanding my knowledge base and never stop learning. Here's to my successful journey towards becoming a DevOps professional! Cheers!

Top comments (0)