Basic Linux Shell Scripting for DevOps Engineers:

Basic Linux Shell Scripting for DevOps Engineers:

Contents:

1.What is shell scripting?

2.What are the different types of shells in Linux?

3.What is Shell Scripting for DevOps?

4.What is #!/bin/bash? Can we write #!/bin/sh as well?

5.Task: Write a Shell Script which prints I will complete #90DaysOofDevOps challenge.

6.Task: Store the output of the command “hostname” in a variable. Display “This script is running on "_.” where “_” is the output of the “hostname” command.

7.Write an example of If else in Shell Scripting by comparing name and value.

What is shell scripting?

Shell scripting in Linux is a method to create scripts or programs that can be executed in the Linux shell. The Linux shell is a command-line interface (CLI) that allows users to interact with the operating system by typing commands. Shell scripts are text files that contain a series of commands that the shell can execute.

What are the different types of shells in Linux?

In Linux, there are different "shells," which are like flavors of the command-line interface. Each shell offers slightly different features and ways to interact with the system. Here are a few common ones explained in simple terms:

  1. Bash (Bourne Again Shell):

    • This is one of the most popular and widely used shells.

    • It's known for its flexibility and extensive features.

    • Bash is often the default shell on many Linux systems.

    • Think of it as the versatile all-rounder.

  2. sh (Bourne Shell):

    • The original shell that inspired many others.

    • It's simple and efficient but lacks some advanced features.

    • Think of it as the classic and reliable choice.

  3. zsh (Z Shell):

    • A modern and feature-rich shell.

    • It offers helpful features for customization and productivity.

    • Think of it as the stylish and efficient option.

  4. fish (Friendly Interactive Shell):

    • Designed to be user-friendly with auto-suggestions and colourful prompts.

    • It's great for beginners and those who appreciate a more intuitive interface.

    • Think of it as a user-friendly and supportive buddy.

  5. csh (C Shell):

    • Offers a syntax similar to the C programming language.

    • It's good for specific tasks, but not as widely used.

    • Think of it as the C-programmer's shell.

  6. Dash:

    • Dash is minimalistic and efficient, often used for system scripts and boot scripts.

    • It's lightweight and suitable for quick operations, but not ideal for interactive use due to limited features.

    • Think of Dash as a speedy shell for system-level tasks.

      What is Shell Scripting for DevOps?

Shell scripting in DevOps is like having a magic wand for automating and streamlining software development and operations tasks. It's a way to write instructions in a file that a computer can run all at once, instead of typing out each command by hand. This helps in making repetitive tasks quick and error-free, like setting up servers, deploying software, or checking system status.

In essence, shell scripting for DevOps is about simplifying and automating the tasks involved in building, deploying, and maintaining software systems, ultimately saving time and reducing the chance of human errors.

What is #!/bin/bash? Can we write #!/bin/sh as well?

#!/bin/bash is known as a shebang (or hashbang, pound-bang, or hash-pling). It's the first line you'll often see at the top of a shell script in Linux and Unix-like systems. This line tells the system which interpreter should be used to execute the script that follows. In the case of #!/bin/bash, it specifies that the script should be run using the Bash shell, which is located at /bin/bash.

Here's a breakdown of its components:

  • #: This character starts a comment in many scripting and programming languages. However, when combined with ! as the first two characters in a script, it has a special meaning and is not treated as a comment.

  • !: This character, when used with # at the beginning of a script, forms the shebang.

  • /bin/bash: This is the path to the Bash shell binary. It specifies that the Bash shell should be used to interpret the script.

You can use #!/bin/sh at the beginning of a shell script. This line tells the computer to use the sh shell (also known as the Bourne shell) to run the script. It's similar to #!/bin/bash, but it specifies a different shell.

The sh shell is more basic and widely compatible across different Unix-like systems compared to bash, which is an enhanced version of sh with more features. While bash includes additional functionalities that are not present in sh.

In simple terms, using #!/bin/sh is like choosing a simpler, more universal tool that works everywhere while #!/bin/bash is like opting for a more advanced tool that has more features but might not be available in every toolbox.

Write a Shell Script which prints I will complete #90DaysOofDevOps challenge.

  1. Write the command into the text editor.(on Linux/macOS)

  2. Save the file with a .sh extension, such as devops_challenge.sh.

  3. Open a terminal.

  4. Navigate to the directory where you saved the script.

  5. Make the script executable by running the command in the terminal: chmod 700 devops_challenge.sh

  6. Run the script by typing: ./devops_challenge.sh in the terminal.

  7. Run the script by typing: ./devops_challenge.sh in the terminal.

Task: Store the output of the command “hostname” in a variable. Display “This script is running on "_.” where “_” is the output of the “hostname” command.

The output:

Here on example-host would be replaced by $HOSTNAME i.e whatever be the name of your host.

Task: Write an example of If else in Shell Scripting by comparing name and value.

Output: