Skip to main content

Command Palette

Search for a command to run...

(Day-4) :Basic Linux Shell Scripting for DevOps Engineers

Published
โ€ข3 min read
(Day-4) :Basic Linux Shell Scripting for DevOps Engineers
S

Motivated and goal-oriented aspiring DevOps engineer with a strong passion for optimizing software development and deployment processes

Hi DevOps Learners, we are back with #Day4 of the #90daysofdevops series and today we will be taking a look at the basics of Shell scripting that is needed if you are going to start your career as a DevOps engineer.

  • Before deep diving into shell commands and scripting let's take a look at what is a Shell and some terms that we need to know to understand these concepts:

What is Kernel?

๐ŸŒฑ A kernel is like the core ๐Ÿง  of an operating system. It is the essential component that manages the system's resources and acts as a bridge ๐ŸŒ‰ between software and hardware.

The kernel is responsible for handling tasks such as memory management ๐Ÿงฎ, process management ๐Ÿ”„, device drivers ๐Ÿ–ฅ๏ธ, and file system access ๐Ÿ“‚. It provides an interface for software applications to interact with the underlying hardware and ensures that different components of the system can work together harmoniously.


What is a Shell? ๐Ÿš

  • ๐Ÿš A shell in Linux is like a friendly ๐Ÿค intermediary between you (the user) and the operating system. It provides a command-line interface (CLI) ๐Ÿ“œ where you can type commands and perform various tasks. It's like a virtual ๐Ÿšช doorway to the system's resources.

  • There are different types of shells available, like the ๐Ÿš Bourne Shell (sh), the widely-used ๐ŸŒŸ Bash (Bourne Again Shell), and the ๐Ÿš C Shell (csh). Each has its own set of features and syntax.


What is Linux Shell Scripting? ๐Ÿง

  • ๐Ÿง Linux shell scripting is a combination of text-based commands, special symbols, and scripting constructs to create powerful scripts that can automate tasks and unleash the potential of the command-line interface (CLI).

Please see below architectural diagram to understand the Linux architecture on how the shell, the kernel, the hardware and utilities coordinate with each other :


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

  • The #!/bin/bash is called a shebang or a hashbang. It is a special construct used at the beginning of a script file to specify the interpreter or shell that should be used to execute the script. In this case, #!/bin/bash indicates that the script should be executed using the Bash shell.

  • By specifying #!/bin/bash, you ensure that the script is interpreted using the Bash shell.

  • While #!/bin/bash is commonly used, it is also possible to use #!/bin/sh as the shebang in your script. This indicates that the script should be executed using the default system shell, which may be a different shell than Bash.


Write a Shell Script which prints

I will complete #90DaysOofDevOps challenge

#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"


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

#!/bin/bash
echo "$@"
echo "Enter the challenge day number :"
read cnumber
echo "Good, lets get started with $cnumber"


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

#!/bin/bash

echo "Enter first number:"
read num1
echo "Enter second number:"
read num2

if [ $num1 -eq $num2 ]; then
        echo "Numbers are equal"
elif [ $num1 -lt $num2 ]; then
        echo " $num1 is smaller than $num2"
else
        echo " $num1 is greater than $num2"
fi

Output in Terminal :


That's it for #day4. In the upcoming days we will be learning about topics like user management, file permissions, access control lists and many more. Till then, happy learning !!! Ciao!