Day 4 Task: Basic Linux Shell Scripting for DevOps Engineers.
#Day4 #90DaysOfDevops #ShellScripting
Table of contents
No headings in the article.
What is Kernel?
Kernel is a central component of an operating system that manages the operations of the computer and hardware. It manages operations of memory and CPU time. It is a core component of an operating system. Kernel acts as a bridge between applications and data processing performed at the hardware level using inter-process communication and system calls.
Basic Linux Shell Scripting
A shell script is a list of commands in a computer program that is run by the Unix shell which is a command line interpreter. A shell script usually has comments that describe the steps. The different operations performed by shell scripts are program execution, file manipulation and text printing. A wrapper is also a kind of shell script that creates the program environment, runs the program etc.
Types Of Shells
There are two major types of shells in Unix. These are:
- Bourne Shell
This is the default shell for version 7 Unix. The character $ is the default prompt for the bourne shell. The different subcategories in this shell are Korn shell, Bourne Again shell, POSIX shell etc.
- C Shell
This is a Unix shell and a command processor that is run in a text window. The character % is the default prompt for the C shell. File commands can also be read easily by the C shell, which is known as a script.
Capabilities Of Shell Script.
The different capabilities of the shell script are −
Batch jobs
Several commands that would be entered manually in a command line interface can be executed automatically using a shell script. This can be done without the user needing to trigger each command separately.
Programming
There are many features in modern shell scripts that are only found in sophisticated programming languages such as arrays, variables, comments etc. Many complicated applications can be written in shell scripts using these features. But there is a problem i.e. shell script languages don’t support classes, threading etc.
Generalisation
It is much more flexible to use loops, variables etc for multiple tasks in a shell script. An example of this is a Unix shell script known as bash, which converts jpg images to png images.
Shortcuts
There is a shortcut provided by a shell script for a system command where command options, environment settings or post-processing apply. This still allows the shortcut script to act as a Unix command.
Advantages of Shell Script:
Some of the advantages of shell script are −
The commands and syntax of the shell script are the same as that entered at the command line. Because of this, there is no need to switch to a completely different syntax.
It is much faster to write code in shell script than in other programming languages. This also means that the program is easier to create and files required can be selected easily.
A shell script can also be used to provide linkage for already existing programs.
Shell scripting can be used by users that are not experts to modify and tailor the behaviour of their programs according to their requirements.
Disadvantages of Shell Script.
Some of the disadvantages of shell script are −
There may be errors in shell scripting that prove to be quite costly.
The programs in shell script are quite slow while executing and a new process is required for every shell command executed.
Different platforms in shell scripting may also have compatibility problems.
What is #!/bin/bash?
The "#!/bin/bash" at the beginning of a script is called a shebang, also known as a hashbang or a sharp bang
. It is used to specify the interpreter that should be used to run the script.
In this particular case, "#!/bin/bash" specifies that the script should be interpreted using the Bash shell, which is a popular command-line shell used on many Unix-like systems. The shebang is usually followed by the path to the interpreter binary, which in this case is "/bin/bash".
When the script is executed, the operating system looks for the shebang at the beginning of the script and uses it to determine which interpreter to use to execute the script. So, in summary, "#!/bin/bash" is a shebang used to specify the interpreter to use when executing a Bash script.
Can we write #!/bin/sh
as well?
you can also use "#!/bin/sh" as the shebang in a script. This specifies that the script should be interpreted using the Bourne shell, which is another popular shell on Unix-like systems.
In fact, "#!/bin/sh" is a more generic shebang that can be used to specify any POSIX-compliant shell, which includes not only the Bourne shell but also shells such as Bash, Zsh, Ksh, and others that comply with the POSIX standard.
Using "#!/bin/sh" can be useful if you want to ensure that your script is portable and can be executed on any system that supports POSIX-compliant shells. However, if you specifically need to use Bash features in your script, it's better to use "#!/bin/bash" instead to ensure that the script is interpreted using Bash.
Write a Shell Script which prints I will complete #90DaysOofDevOps challenge
To write a script for #90daysOfDevops challenge, we need to follow the steps as below:
Step 1. Use any editor like vi or gedit to write a shell script.
Step 2. We will give the name to File as "FirstShellScript.sh"
Step 3. Give a command as a vi FirstShellScript.sh.
Step 4. You will be inside the vi editor and you need to use Insert mode.
Step 5. To start the insert mode, press I button on Keyborad.
Step 6. Type #!/bin/bash and then press enter and write a echo command as a echo"I will complete #90DaysOfDevopsChallenge". Please screen shot as below:
Step 7. Then press ESC button and:wq! and enter the key.
Step 8. Set the permissions for FirstShellScript.sh as below:
Step 9: Run the file FirstShellScript.sh and you will get the output as below:
Write a Shell Script to take user input, input from arguments and print the variables.
Shell script for user input as below:
Output as below:
Write an Example of If else in Shell Scripting by comparing 2 numbers.
Output: