Day 4 : Basic Linux Shell Scripting for DevOps

Day 4 : Basic Linux Shell Scripting for DevOps

ยท

3 min read

Introduction to Shell Scripting for DevOps ๐Ÿš€

Welcome to Day 4 of our 90daysofDevOps journey! Today, we're diving into the fascinating world of shell scripting. Shell scripting is a powerful tool for automating tasks, managing system operations, and simplifying complex workflows. As DevOps professionals, mastering shell scripting can greatly enhance our efficiency and productivity. Let's explore what shell scripting is and how to use it effectively. ๐Ÿ–ฅ๏ธ

What is Shell Scripting for DevOps? ๐Ÿค”

Shell scripting is the practice of writing scripts for the shell, which is the command-line interpreter for Unix-based systems like Linux. A shell script is a text file containing a series of commands that the shell can execute sequentially. Shell scripts are used to automate repetitive tasks, manage system configurations, and streamline DevOps processes.

What is #!/bin/bash? ๐Ÿค”

The #!/bin/bash line at the beginning of a shell script is called a shebang. It specifies the interpreter that should be used to execute the script. In this case, /bin/bash indicates that the script should be run using the Bash shell.

Yes, you can write #!/bin/sh as well, which indicates that the script should be run using the Bourne shell. However, Bash (Bourne Again SHell) is more common and offers additional features and enhancements over the original Bourne shell.

Writing a Shell Script to Print a Message ๐Ÿ“œ

Let's write a simple shell script that prints a message:

But when you run it permission gets denied. This is because currently your file is not executable. To run it you need to give all the permission for execution. To more about file permission you can refer my previous blog :
https://hashnode.com/post/cly5zkm9a00010akw5fn758qs

To run this script:

  1. Save it as devops.sh.

  2. Make it executable: chmod 777devops.sh.

  3. Execute it: ./devops.sh.

Writing a Shell Script to Take User Input and Arguments ๐Ÿ“ฅ

This script will prompt the user for input and also accept arguments from the command line:

To run this script:

  1. Save it as variable.sh.

  2. Make it executable: chmod +x variable.sh

  3. Execute it with arguments: ./variable.sharg1_value arg2_value.

Example of If-Else in Shell Scripting ๐Ÿ”„

Let's write a script to compare two numbers and print which one is greater:

Below are the two conditions

  1. Where number 1 is less than number 2

  2. Both the numbers are equal

Conclusion ๐ŸŒŸ

Shell scripting is an essential skill for any DevOps professional. It enables us to automate tasks, manage systems efficiently, and handle complex workflows with ease. By mastering the basics of shell scripting, we're laying a strong foundation for our DevOps journey. Keep practicing and experimenting with different scripts to enhance your skills. Stay tuned for more exciting lessons

Happy scripting! ๐Ÿ“œโœจ


Feel free to reach out with any questions or thoughts in the comments below. Happy DevOps-ing! ๐Ÿš€

ย