Day4-Basic Linux Shell Scripting for DevOps

Introduction to Linux Shell and Shell Scripting

Day4-Basic Linux Shell Scripting for DevOps

While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understanding shell scripting we have to get familiar with the following terminologies:

• Kernel

• Shell

• Terminal

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. It manages the following resources of the Linux system –

• File management

• Process management

• I/O management

• Memory management

• Device management etc.

It is often mistaken that Linus Torvalds has developed Linux OS, but actually, he is only responsible for the development of the Linux kernel.

Complete Linux system = Kernel + GNU system utilities and libraries + other management scripts + installation scripts.

What is Shell?

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

Shell is broadly classified into two categories –

• Command Line Shell

• Graphical shell

Command Line Shell

Shell can be accessed by users using a command line interface. A special program called Terminal in Linux/macOS, or Command Prompt in Windows OS is provided to type in the human-readable commands such as “cat”, “ls” etc. and then it is being executed. The result is then displayed on the terminal to the user. A terminal in Ubuntu 16.4 system looks like this –

Linux command line

In the above screenshot “ls” command with “-l” option is executed. It will list all the files in the current working directory in a long listing format. These files are usually called batch files in Windows and Shell Scripts in Linux/macOS systems.

There are several shells are available for Linux systems like

• BASH (Bourne Again SHell) – It is the most widely used shell in Linux systems. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.

• CSH (C SHell) – The C shell’s syntax and its usage are very similar to the C programming language.

• KSH (Korn SHell) – The Korn Shell was also the base for the POSIX Shell standard specifications etc.

Each shell does the same job but understands different commands and provides different built-in functions.

Graphical Shell

Graphical shells provide means for manipulating programs based on the graphical user interface (GUI), by allowing for operations such as opening, closing, moving, and resizing windows, as well as switching focus between windows. Window OS or Ubuntu OS can be considered as a good example which provides GUI to the user for interacting with the program. Users do not need to type in commands for every action. A typical GUI in the Windows system –

GUI Shell

What is Terminal?

For those having Linux OS, when we click to open the icon having the name “Terminal”, a window is opened, so in simple terms that rectangular frame or window is called terminal.

The terminal is a program that provides the user with a simple command-line interface and performs the following 2 tasks:

• Takes input from the user in the form of commands.

• Displays output on the screen.

What is Shell Scripting?

A shell script is a file that contains a sequence of commands for a LINUX-based operating system. That is typed into the keyboard one at a time, into a single script. A shell script is usually created for command sequences in which a user has a need to use repeatedly in order to save time.

For writing a script we have to create a file using vim or nano editor where we write the script.

.sh is an extension of the script file.

• We can run the script by following commands:

bashscript.sh or by ./script.sh

What is BASH Shell?

A bash is a powerful tool that allows users to interact with the operating system, execute commands, automate tasks, and perform system administration tasks.

In DevOps we mainly use bash shell. We can know which bash we are using and their path also by simply typing which bash command.

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

#!/bin/bash or #!/bin/sh in script file is known as a shebang line or hashbang line. It is used to write on top of the script file.

#!/bin/bash : It is used for enabling bash shell as an interpreter to execute the script. In maximum systems by default bash shell is used.

#!/bin/sh : It is used for the default shell of the system to execute the script which might be different shells like Bourne, and Korn shells.

Write a Shell Script which prints: -

I will complete #90DaysofDevOps challenge.

"echo command" is use for printing the script. To save and exit from the editor we first press the ESC button after that we press ":wq" and after that hit the enter button.

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

→ Here read will take input from the user. ${your} will read the value which the user provides.

→ Here it will print the input provided by the user.

$1 is our first argument and $2 is our second argument which is written after our script file name.

→ Here it will print the first argument "27".

→ Here it will print the second argument "Chennai".

→ $name will take the variable input.

→ Here it prints the variable.

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

→ It will check if the input argument is odd or even.

→ It shows the value is even or odd according to the argument.

This is the #Day04 of the #90DaysofDevOps challenge! Hope you found this article informative and useful so please share it with others who might benefit from it.

Thanks for reading this article.

Keep Learning...