Day4- Devops Journey.
Basic Linux Shell Scripting for DevOps Engineer.
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.
Task1:What is Shell Scripting:
Linux Shell Scripting is a programming method that offers a way to interact with the operating system.
In Linux, Shell is a command-line interface that interprets the commands given by the user to the kernel of the system. Shell Scripting, on the other hand, is a program that contains a series of commands for the shell to execute.
Types of Shell Scripting:
Shell scripting, which is essentially a sequence of commands for the shell to execute, can be written in several types of shells. Here are a few of the more commonly used types:
Bourne Shell (sh): This is the original shell created by Steve Bourne. Scripts written in this shell are interpreted, not compiled.
Korn Shell (ksh): This is a high-level programming shell created by David Korn. It includes features from both the Bourne Shell and C shell, including command history, command aliasing, and more.
C Shell (csh): This shell was developed by Bill Joy for the BSD version of Unix. It includes syntax and functions similar to those in the C programming language.
Bourne Again SHell (bash): This is a superset of the Bourne Shell, developed for the GNU project. It is the default shell for most Unix-like systems and includes features from the Bourne Shell, Korn Shell, and C Shell.
Z Shell (zsh): This shell is similar to the Korn Shell but includes many enhancements. It's highly customisable and includes features like spell checking, a shared command history, and more.
What is Script?
A script is a sequence of programs that automates tasks and operations.
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:
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 command
which bash
command.
What is Terminal:
It is a program that establishes a connection with the server.
It is used for accessing the shell.
Task2: What is#!/bin/bash?
can we write#!/bin/sh
as well?
!/bin/bash is known as a shebang or a hashbang. It's a specific sequence of characters at the beginning of a script that specifies the interpreter for the script. In this case, the interpreter is the Bourne Again Shell (bash).
When a script with #!/bin/bash at the start is run, it will be executed in the bash shell, regardless of the current shell of the terminal. It's important to note that the shebang must be the very first thing in the file, with no spaces or blank lines before it.
can we write#!/bin/sh
as well? - Yes, we can write #!/bin/sh in a script.
Task3: Write a Shell Script that printsI will complete #90DaysOofDevOps challenge
Open terminal > type #vim scriptname.sh
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"
By using the echo command we print. 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.
w- save , q- quit.
To view the output, we can use bash script.sh or ./script.sh
Task4: Write a Shell Script to take user input, input from arguments and print the variables.
#vim 3rdscript.sh
-> Here read will take input from the user. ${you} will read the value which the user provides.
Task5: Write an Example of If else in Shell Scripting by comparing 2 numbers.
#vim 4thscript.sg
#!/bin/bash
echo "Enter first number" read num1 echo "Enter second number" read num2
if [ $num1 -gt $num2 ] then echo "$num1 is greater than $num2" elif [ $num1 -eq $num2 ] then echo "Both numbers are equal" else echo "$num2 is greater than $num1" fi
:wq!
output-