โœ๏ธDay 4: Basic Linux Shell Scripting for DevOps Engineers

ยท

4 min read

โœ๏ธDay 4: Basic Linux Shell Scripting for DevOps Engineers

Table of content

  • What is Kernel?

  • What is Shell?

  • What is Linux Shell Scripting?

  • What is Shell Scripting for DevOps with Examples?

  • Getting started with a simple script ๐Ÿš€

  • Basic Shell scripting concepts๐Ÿ“š

  • Real-world examples๐ŸŒ

  • Conclusion๐ŸŽฏ

โœ๏ธWhat is Kernal?

Kernel is the core of an operating system. It acts as a bridge between the hardware and software, managing system resources and allowing communication between them. Think of it as the brain ๐Ÿง  of your computer, handling all the essential tasks to keep everything running smoothly.

โœ๏ธWhat is Shell?

Shell is a command-line interface (CLI) that allows users to interact with the operating system by typing commands. It acts as a mediator between the user and the kernel, translating user commands into actions performed by the kernel.

โœ๏ธWhat is Linux Shell Scripting?

Shell script is a program written for the shell, or command line interpreter, of an operating system. Think of it as a list of commands that you can save in a file and execute whenever you need to. It's like writing a recipe ๐Ÿ“ for the computer to follow.

โœ๏ธWhat is Shell Scripting for DevOps with Examples?

Shell scripting for DevOps is like having a digital assistant that helps you automate repetitive tasks and manage your software development and operations more efficiently. It's a way to write small programs (scripts) using commands that your computer understands, allowing you to automate tasks such as setting up servers, deploying software, or checking system health.

Here are some simple examples:

  1. Automating Deployment: Imagine you have to deploy your application to several servers every time you make changes. Instead of doing it manually each time, you can write a script that does it for you automatically.

  2. Infrastructure Setup: If you need to create virtual machines or set up cloud resources frequently, a script can handle that process, saving you time and effort.

  3. Backup and Maintenance: You can write scripts to back up your databases or files regularly, ensuring that your data is safe. You can also create scripts to monitor system health and alert you if something goes wrong.

  4. CI/CD Pipelines: In software development, continuous integration and continuous deployment (CI/CD) pipelines automate the building, testing, and deployment of code changes. Shell scripts can help orchestrate these pipelines, making the process smooth and efficient.

  5. System Monitoring: Scripts can continuously check system performance metrics and notify you if any thresholds are exceeded, allowing you to take proactive action to avoid potential issues.

Getting Started with a Simple Script ๐Ÿš€

Let's write a simple script that prints "Hello, DevOps World!" to the terminal.

  1. Open your favorite text editor (like nano or vim) ๐Ÿ“

  2. Type the following script:

     #!/bin/bash
     echo "Hello, DevOps World! ๐ŸŒ"
    
  3. Save the file as hello.sh ๐Ÿ’พ

  4. Make the script executable by running:

     chmod +x hello.sh
    
  5. Execute the script by running:

     ./hello.sh
    

And there you go! Youโ€™ve just written and executed your first shell script. ๐ŸŽ‰

Basic Shell Scripting Concepts ๐Ÿ“š

Variables ๐Ÿ“ฆ

Variables store data that can be used and manipulated throughout your script. Hereโ€™s how to use them:

#!/bin/bash
name="Ritesh"
echo "Hello, $name! ๐Ÿ‘‹"

Loops ๐Ÿ”„

Loops help you repeat tasks efficiently. Hereโ€™s an example of a for loop:

#!/bin/bash
for i in 1 2 3 4 5
do
  echo "Welcome $i times! ๐Ÿ™Œ"
done

Conditional Statements ๐Ÿ”

Conditional statements allow you to make decisions in your script:

#!/bin/bash
num=10
if [ $num -gt 5 ]; then
  echo "$num is greater than 5 ๐Ÿ‘"
else
  echo "$num is not greater than 5 ๐Ÿ‘Ž"
fi

Real-World Examples ๐ŸŒ

1) Backup Script ๐Ÿ—‚๏ธ

Hereโ€™s a script to back up a directory:

#!/bin/bash
src="/home/user/documents"
dest="/home/user/backup"
cp -r $src $dest
echo "Backup completed successfully! โœ…"

2) Monitoring Script ๐Ÿ“Š

A script to check if a service is running:

#!/bin/bash
service="nginx"
if pgrep -x "$service" >/dev/null
then
  echo "$service is running ๐ŸŸข"
else
  echo "$service is not running ๐Ÿ”ด"
fi

Conclusion ๐ŸŽฏ

Shell scripting is an essential skill for DevOps engineers. It allows you to automate and streamline your workflows, making you more efficient and productive. ๐Ÿš€ Keep experimenting, and soon youโ€™ll be writing scripts to handle all sorts of tasks! ๐Ÿ’ช

Stay tuned for Day 5, where weโ€™ll explore more exciting DevOps topics. ๐Ÿ“…

Happy scripting! ๐Ÿ˜Š

ย