Basic Shell Scripting for DevOps

Basic Shell Scripting for DevOps

Introduction

Welcome to Linux shell scripting! In this guide, we will walk you through the basics of writing simple scripts using Bash, one of the most popular shells in Linux. You’ll learn how to automate tasks like creating files, displaying system information, and saving time by writing a script that does all the work for you.


What Will We Learn Today?

In this blog, we’ll cover:

  • What Linux shell scripting is and how it works.

  • The difference between the kernel and the shell.

  • How to write a simple Bash script to automate tasks.

  • How to create directories and files through a script. By the end, you’ll have a working script that automates basic system tasks!


What is a Shell?

A shell is a program that allows you to interact with your computer. When you type commands like ls or cd, the shell processes them and sends them to the kernel. The shell is your way to give instructions to the computer and get things done.


What is the Kernel?

The kernel is the core part of the Linux operating system. It manages the computer’s resources, like memory and CPU, and makes sure everything runs smoothly. When the shell sends commands to the kernel, the kernel handles them by interacting with the computer’s hardware (like opening files, running programs, etc.).


What is Shell Scripting?

Shell scripting is the process of writing commands in a file, called a script, so the computer can execute them all at once. Instead of typing each command manually, you can write them down and run them together. This is helpful for automating tasks and reducing repetitive work.


Benefits of Using Shell Scripting

Here’s why learning shell scripting can be really useful:

  1. Automate repetitive tasks: You can save time by creating scripts to run commands automatically.

  2. Simplify complex tasks: Instead of typing multiple commands, you can put them in one script.

  3. Reduce errors: Scripts ensure that the same commands are executed consistently every time.

  4. Useful in DevOps: DevOps professionals use shell scripts to manage servers and automate tasks efficiently.


Types of Shells in Linux

There are different types of shells in Linux. Here are a few popular ones:

  1. Bash (Bourne Again Shell): The most commonly used shell in Linux.

  2. Zsh (Z Shell): Offers extra features and customization.

  3. Fish (Friendly Interactive Shell): Known for being easy to use with helpful suggestions.

  4. Csh (C Shell): A shell with a syntax similar to the C programming language.

  5. Ksh (Korn Shell): Often used in Unix systems.

Which Shell is Commonly Used?

Most Linux systems use Bash by default. It’s powerful, easy to use, and supported across many platforms. Learning Bash is a great start for anyone interested in scripting or working in a Linux environment.

The Shebang: #!/bin/bash

When writing a shell script, the shebang (#!) is very important. It tells the system which shell to use when running the script. In this case, we’ll use Bash, so the shebang looks like this:

Press i to enter insert mode in vim, and write the following code:

What Do These Commands Do?

  • #!/bin/bash: The shebang that tells the system to use Bash to run the script.

  • df -h: Displays the disk usage of your system.

  • date: Shows the current date and time.

  • whoami: Prints the username of the person running the script.

  • mkdir devops: Creates a new directory named "devops."

  • cd devops: Changes into the newly created "devops" directory.

  • touch file{1..5}.txt: Creates five files named file1.txt to file5.txt in the "devops" directory.

Step 2: Save and Exit vim

To save the file and exit vim, press Esc, then type:

This will save your changes and close the editor.

Step 3: Make the Script Executable

Before you can run your script, you need to make it executable. You can do this by typing:

  • chmod: This command changes the file's permissions.

  • +x: This option adds executable permission.

  • script.sh: Replace this with the name of your script file.

By running this command, you allow the system to execute your script!

This command gives your script permission to run.

Step 4: Run the Script

Now you’re ready to run the script! Use the following command to execute it:

When you run the script, it will:

  1. Display your system’s disk usage,

  2. Show the current date and time,

  3. Print your username,

  4. Create a new directory called "devops",

  5. Inside the "devops" directory, it will create five files named file1.txt, file2.txt, file3.txt, file4.txt, and file5.txt.

Conclusion

You’ve just written and run your first Bash script! This simple script automates the process of displaying system information and creating files. Now that you understand the basics of shell scripting, you can start writing your own scripts to automate everyday tasks and make your workflow easier.

With a little practice, you’ll be able to write more complex scripts and explore the power of Linux shell scripting!