Day 4 -90Day of DevOps Challenge

Day 4 -90Day of DevOps Challenge

Basic Shell Scripting

A user logs in to the system or opens a console window, the kernel runs a new shell instance. The kernel is the heart of any operating system. It is responsible for the control management, and execution of processes, and ensuring proper utilization of system resources.

Shell is a program that acts as an interface between a user and the kernel. It allows a user to give commands to the kernel and receive responses from it. Through a shell, we can execute programs and utilities on the kernel. Being able to interact with the kernel makes shells a powerful tool. Without the ability to interact with the kernel, a user cannot access the utilities offered by their machine’s operating system.

Kernal:

Linux kernel is a free, open-source, monolithic, modular, Unix-like operating system kernel. It is the main component of the Linux operating system (OS) and is the core interface between the computer’s hardware and its processes. Its main role is to manage hardware resources for users and is generally used to provide an interface for user-level interaction. A kernel is the first program that is loaded whenever a computer system starts. It is also referred to as low-level system software.

Shell Scripting:

Shell scripting involves writing scripts or programs using a command-line interface to automate tasks, manage resources, and perform operations in an operating system.

Consider a list of daily responsibilities, including checking your email, backing up essential documents, and updating your schedule. You can create a shell script containing the instructions for each of these daily chores instead of having to perform them manually every day. When you run the script, the shell goes through each directive individually and completes the duties automatically, so you don't have to. Shell scripting is a necessary skill for anyone working with Unix-based operating systems due to its versatility, portability, and effectiveness.

Types of shell scripting:

  1. Bash shell: This is the most commonly used shell by effective users in Linux and is the default shell for most Linux distributions. It is a powerful and versatile shell that is compatible with many Unix commands.
  •     Path Name: 
        /bin/sh or /sbin/sh
    

    2. Zsh shell (Zsh): This is a powerful and feature-rich shell with many advanced features such as spell checking, globbing, and command line editing. It is also highly customizable and has a large user community.

    3. Korn shell (ksh): This is a Unix shell that is compatible with the Bourne shell (sh) and offers many additional features to the current users such as command line editing, spelling correction, job control, and arithmetic expressions.

    4. C shell (csh): This is a Unix shell that has a syntax similar to the C programming language. It includes many features such as command line editing, error status, fatal errors, history, and job control.

#!/bin/bash?

The line “#!/bin/bash” is known as the shebang or hashbang. It is a special character sequence that tells the system which interpreter to use for executing the script.

#!/bin/bash

In simple words, the she-bang at the head of the script tells the system that this file is a set of commands to be fed to the command interpreter indicated.

Operating systems have a choice of multiple shells. The shell is responsible not only for the little prompts but also interpreting the commands of the script. Thus the shell plays an important role especially when we implement big and complex logic using conditions, pipes, loops etc.

Shell Script Example: Completing the #90DaysOfDevOps Challenge:

  1. Let's write a simple shell script that prints the message "I will complete the #90DaysOfDevOps challenge" when executed:

      vim hello.sh
      echo " I will complete #90DaysOfDevOps Challenge "
    

    This script uses the “echo” command to output the message to the console. The shebang line at the top of the script specifies that Bash should be used as the interpreter.

    To run this script, you can save it to a file with a “.sh” extension (e.g.

    hello.sh”).

Let's write a Shell Script to take user input, input from arguments and print the variables

This script prompts the user to enter their name, reads the input into the variable “name”, and then assigns the first two command-line arguments to the variables “arg1” and “arg2”. It then uses the “echo” command to print the values of these variables to the console, along with some descriptive text.

To run this script, you can save it to a file with a “.sh” extension with “./arg.sh" where “arg1” and “arg2” are the command-line arguments you want to pass to the script. You will also be prompted to enter your name during execution.

3. Write an Example of If else in Shell Scripting by comparing 2 numbers

#1/bin/bash
echo "enter a number:",
read num
if [num -gt 0 ];then
echo "It's a positive number!"
elif [num -lt 0 ];then
echo "It's a negative number!"
else 
echo "It is a zero"
fi

In this example, first, we use echo, To read the number and print the number. We then use if-else statements to compare these numbers.

First, we check if the condition[ $num1 -gt 0 ]. If this condition is true, we print the message “It's a positive number ”.

If the first condition is false, we move on to the next condition using elif

[ $num1 -lt 0 ]. If this condition is true, we print the message “It is a negative number"

If both of these conditions are false, we move on to the final else statement, which is executed when num1 is equal to num2. In this case, we print the message “It is zero".

To execute this script, you can save it to a file with a “.sh” extension (e.g gt.sh). Here example we entered number 10 so it is a positive number.

Thank you, This is my learning about Shell scripting. Hope you like it.

Happy Learning..!!

Siri Chandana

#devops #90daysofdevops #trainwithshubham #tws #LearningTogether #connections #community #Shellscripting #Linux