Day 4 - Basic Linux Shell Scripting

Day 4 - Basic Linux Shell Scripting

What is Shell Scripting for DevOps?

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

A shell is a special user program which provide an interface to the user to use operating system services. Shell accept human-readable commands from 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.

Shell is broadly classified into two categories –

  • Command Line Shell

  • Graphical shell

Shell Scripting is an open-source computer program designed to be run by the Unix/Linux shell. Shell Scripting is a program to write a series of commands for the shell to execute. It can combine lengthy and repetitive sequences of commands into a single and simple script that can be stored and executed anytime which, reduces programming efforts

A shell script has syntax just like any other programming language. If you have any prior experience with any programming language like Python, C/C++ etc. it would be very easy to get started with it.

A shell script comprises following elements

  • Shell Keywords – if, else, break etc.

  • Shell commands – cd, ls, echo, pwd, touch etc.

  • Functions

  • Control flow – if..then..else, case and shell loops etc.

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

Any shell script is identified by Shebang or hashbang(#!). Shebang is a combination of bash(#) and bang(!), followed by the shell path.
The shebang(#!) at the head of a script tells your system that this file is a set of commands to be fed to the command interpreter.

/bin/sh and /bin/bash are both Unix/Linux shell interpreters, but they have some differences in their behavior and syntax. /bin/sh is a POSIX-compliant shell that is designed to be small and efficient, while /bin/bash is a more feature-rich shell that includes many additional features and extensions.

For example, some features such as arrays and some string operations are not available in /bin/sh, but they are available in /bin/bash. Additionally, /bin/bash provides more extensive support for interactive shell features such as command history, auto-completion, and job control.

We can write #!/bin/sh in place of #!/bin/bash, the difference is just in shells 'sh' means Bourne Shell and 'bash' means Bourne Again Shell. Bash shell scripting is a kind of shell scripting only. You can say, it's a subset of shell scripting. The Bourne Shell is another commonly used shell in Unix-based operating systems.

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

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

Output

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

Thankyou for reading!! many more in a queue

~Nikunj Kishore Tiwari

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe for Guiding Us.

#devops #90daysofdevops #allaboutdevops #linux