Day 4 - #90Days of DevOps challenge Basic Linux Shell Scripting for DevOps Engineers
Table of Contains
What is Kernel
What is Shell
What is Linux Shell Scripting?
Tasks
What is Shell Scripting for DevOps
What is #!/bin/bash? can we write #!/bin/sh as well?
A Shell Script which prints I will complete #90DaysOofDevOps challenge A Shell Script to take user input, input from arguments and print the variables. An Example of If else in Shell Scripting by comparing 2 numbers Was it difficult?
What is Kernel
The kernel is the essential foundation of a computer's operating system (OS). It is the core that provides basic services for all other parts of the OS. It is the main layer between the OS and underlying computer hardware, and it helps with tasks such as process and memory management, file systems, device control and networking.
During normal system startup, a computer's basic input/output system, or BIOS, completes a hardware bootstrap or initialization. It then runs a bootloader which loads the kernel from a storage device -- such as a hard drive -- into a protected memory space. Once the kernel is loaded into computer memory, the BIOS transfers control to the kernel. It then loads other OS components to complete the system startup and make control available to users through a desktop or other user interface.
If the kernel is damaged or cannot load successfully, the computer will be unable to start completely -- if at all. This will require service to correct hardware damage or restore the operating system kernel to a working version.
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.
What is Linux Shell Scripting?
The shell is a program that provides the user with an interface to use the operating system’s functions through some commands. A shell script is a program that is used to perform specific tasks.
Shell scripts are mostly used to avoid repetitive work. You can write a script to automate a set of instructions to be executed one after the other, instead of typing in the commands one after the other n number of times.
In this article, we’ll go through some commands for BASH (Bourne Again SHell), which is the most widely used shell and also the default in most Linux systems, which most servers use as their operating system. Shell scripting is particularly useful for DevOps and backend development.
Exeample
A typical script might look like this:
#!/bin/bash
echo "Welcome!! Please Enter Your Name"
read name
echo "Hello $name"
What is
#!/bin/bash
? can we write#!/bin/sh
as well?
#!/bin/bash is known as a shebang or hashbang, which is the first line of a shell script that specifies the interpreter to be used to execute the script. In this case, it specifies that the Bash shell should be used to interpret and run the script.
Yes, it is also possible to use #!/bin/sh as the shebang line. The sh shell is a standard shell interpreter that is available on most Unix-like systems, and it is usually a symlink to another shell, such as Bash, Dash, or KornShell. Using #!/bin/sh as the shebang line will make the script compatible with any shell that supports the POSIX standard.
However, there are some differences between the sh shell and Bash, so if the script relies on features that are specific to Bash, such as arrays or extended globbing, it should use #!/bin/bash instead. Additionally, if the script is expected to run on a system where sh is not symlinked to Bash, it may be necessary to use #!/bin/bash to ensure that the script runs as expected.
Write a Shell Script which prints I will complete
#90DaysOofDevOps
challenge
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"
To run the script, save it to a file with a .sh extension (e.g., script.sh), navigate to the directory containing the file in the terminal, and run the following command:
sh script.sh
This will execute the script, and the output I will complete #90DaysOfDevOps challenge will be printed to the console.
Write a Shell Script to take user input, input from arguments and print the variables.
#!/bin/bash
# Read user input and store it in a variable
echo "Enter your name:"
read name
# Print the variable
echo "User input: $name"
# Store input from arguments in a variable
input=$1
# Print the variable
echo "Input from arguments: $input"
In this script, the read command is used to read user input and store it in the $name variable. The echo command is then used to print the variable to the console.
The input from arguments is also stored in a variable called $input. The $1 in the script refers to the first argument passed to the script. Additional arguments can be accessed using $2, $3, and so on.
To run the script, save it to a file with a .sh extension (e.g., script.sh), navigate to the directory containing the file in the terminal, and run the following command:
sh script.sh chakraborty
This will execute the script and pass the argument John to the script. The output will be:
Enter your name:
saswata
User input: saswata
Input from arguments: chakraborty
Here, the user input is saswata, and the input from the argument is chakraborty.
Write an Example of If else in Shell Scripting by comparing 2 numbers
#!/bin/bash
# Define two numbers
number1=10
number2=5
# Compare the numbers using if-else statement
if [ $number1 -gt $number2 ]; then
echo "$number1 is greater than $number2"
else
echo "$number2 is greater than $number1"
fi
In this script, we define two numbers number1 and number2. We then use an if-else statement to compare the two numbers. The condition [ $number1 -gt $number2 ] checks if number1 is greater than number2. If this condition is true, the code inside the if block will be executed, and the script will print "number1 is greater than number2". Otherwise, the code inside the else block will be executed, and the script will print "number2 is greater than number1".