Basic Linux Shell Scripting for DevOps Engineers.

Basic Linux Shell Scripting for DevOps Engineers.

#day5/90 #devOps

What is Kernel

The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system. A kernel is a piece of software that acts as a bridge between the hardware and software components of a computer system. It is the core component of an operating system, responsible for managing the system's resources and facilitating communication between different software programs and the hardware.

A kernel is a low-level program that is loaded into the computer's memory when the system starts up. It remains in memory throughout the operation of the computer, providing essential services and functionality to other software components.

The kernel performs critical tasks such as managing memory, scheduling tasks, handling input/output operations, and controlling hardware devices like the processor, memory, and peripherals. It acts as an intermediary, allowing software applications to communicate with the hardware without needing to understand the intricate details of the underlying hardware architecture.

What is Shell

A shell is a special user program that provides an interface for the user to use operating system services. Shell accepts human-readable commands from a user and converts them into something which the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or start the terminal.

What is Linux Shell Scripting?

A shell script is a computer program designed to be run by a Linux shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Tasks:

  • Explain in your own words and examples, what is Shell Scripting for DevOps.

    Shell scripting for DevOps involves utilizing shell scripts, along with various tools, to automate and streamline tasks involved in the development, deployment, and management of software applications in a DevOps environment.

    1. Bash (Bourne Again SHell): Bash is a popular shell program used in Unix-like operating systems. It provides a command-line interface for interacting with the operating system and executing commands. Bash scripts, written in the Bash scripting language, allow for the automation of tasks. For example, a Bash script can be used to automate the deployment of a web application by executing commands to pull code from a repository, build the application, and deploy it to a server.

    2. Ansible: Ansible is a powerful automation tool that uses SSH to remotely manage and configure systems. It allows you to define infrastructure as code using YAML files, which can be version controlled and easily shared among team members. Ansible playbooks, written in YAML, can execute shell scripts or commands on remote systems, enabling tasks like server provisioning, configuration management, and application deployment.

    3. Docker: Docker is a popular containerization platform that allows you to package applications and their dependencies into containers. Shell scripts are often used in conjunction with Docker to define the steps needed to build, configure, and deploy containerized applications. Dockerfiles, which are text files containing instructions for building Docker images, can execute shell commands to set up the application environment and install required dependencies.

    4. Jenkins: Jenkins is an open-source automation server that provides a platform for continuous integration and continuous delivery (CI/CD). With Jenkins, shell scripts can be used within build pipelines to automate various stages of the software development lifecycle. For example, a Jenkins pipeline can execute shell commands to build the application, run tests, and deploy it to a testing or production environment.

    5. Terraform: Terraform is an infrastructure-as-code tool that enables you to define and manage cloud resources and infrastructure. It uses a declarative configuration language to describe the desired state of your infrastructure. Shell scripts can be used within Terraform modules or provisioners to perform additional customizations or execute commands on provisioned resources.

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

    #!/bin/bash indicates that the script should be executed using the Bash shell.

    while, #!/bin/sh specifies the system's default Bourne shell or its compatible equivalent. yes, you can write #!/bin/sh as well. The sh, the shell is often a symbolic link or a smaller, simpler shell that aims to be compatible with the original Bourne shell.

  • Write a Shell Script that prints Hello world

    Steps:

    a. create a "file. sh" using the vim command:

      $vim Greet.sh
    

    File:

    b. Make sure you give the execute permission to the shell file:

      $ ls Greet.sh -al
    

    Using the "chmod" command, give execute permission to the owner.

      $ chmod 753 Greet.sh
    

    c. Run the shell file:

      $ ./Greet.sh
    

  • Write a Shell Script to take user input, input from arguments and print the variables.

      #!/bin/bash
    
      # Take user input
      read -p "Enter a value: " userInput
    
      # Check if any command line arguments were provided
      if [ $# -gt 0 ]; then
          argInput=$1
      else
          argInput="No argument provided"
      fi
    
      # Print the variables
      echo "User Input: $userInput"
      echo "Argument Input: $argInput"
    
  • Write an Example of If else in Shell Scripting by comparing 2 numbers.

      #!/bin/bash
    
      # Take user input for num1
      read -p "Enter the first number (num1): " num1
    
      # Take user input for num2
      read -p "Enter the second number (num2): " num2
    
      # Compare the numbers
      if [ $num1 -gt $num2 ]; then
          echo "$num1 is greater than $num2"
      elif [ $num1 -lt $num2 ]; then
          echo "$num1 is less than $num2"
      else
          echo "Both numbers are equal"
      fi
    

    The -p option is used to display the prompts for num1 and num2.

  • Here's the breakdown of the if-else statement:

    • if [ $num1 -gt $num2 ]; then: This line checks if num1 is greater than num2.

    • elif [ $num1 -lt $num2 ]; then: If the previous condition is false, this line checks if num1 is less than num2.

    • else: If both the previous conditions are false, this line is executed, indicating that the numbers are equal.