Shell Scripting
We all remember the scene from the movie 3 Idiots where Rancho was doing something to the college's computer system.He was basically doing shell scripting.
But Hey there are more beneficial uses for shell scripting which can be used for good purposes.
Shell: A shell is a command-line interpreter that allows users to interact with the operating system.
Scripting: Scripting is the process of writing a computer program that is executed by a shell.
So, shell scripting is the process of writing a computer program that is executed by a shell. Shell scripts are typically written in a scripting language
Types
Most common ones are
Bourne shell scripts: Bourne shell scripts are the oldest and most basic type of shell script. They are typically written in the Bourne shell language, which is a simple scripting language that is available on most Unix and Linux systems.
Bourne Again shell scripts: Bourne Again shell scripts (Bash scripts) are a more advanced type of shell script that are written in the Bash shell language. Bash is a superset of the Bourne shell language, so Bash scripts can be run on any system that has a Bash shell installed.
Korn shell scripts: Korn shell scripts are a type of shell script that are written in the Korn shell language. The Korn shell is a more powerful and versatile shell than the Bourne shell, so Korn shell scripts can be used to perform more complex tasks.
C shell scripts: C shell scripts are a type of shell script that are written in the C shell language. The C shell is a shell that is designed to be compatible with the C programming language, so C shell scripts can be used to perform tasks that are similar to those that can be performed in C programs.
Z shell scripts: Z shell scripts are a type of shell script that are written in the Z shell language. The Z shell is a newer shell that is designed to be more powerful and versatile than the Bourne shell, Korn shell, and C shell.
Role in DevOps
Shell scripting for DevOps is the use of shell scripts to automate tasks in the DevOps lifecycle. Shell scripts are typically written in Bash, a command-line scripting language that is available on most Linux and Unix systems. Shell scripts can be used to perform a variety of tasks, such as:
Setting up and configuring development environments
Building and testing code
Deploying code to production
Monitoring and troubleshooting systems
Shell script that prints the message "I will complete #90DaysOfDevOps challenge":
#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge"
Shell script that takes user input, input from arguments, and prints the variables:
#!/bin/bash
# Prompt the user for their name.
echo "What is your name?"
read name
# Get the first argument.
first_arg=$1
# Print the user's name and the first argument.
echo "Your name is $name."
echo "The first argument is $first_arg."
This script will first prompt the user for their name using the read
command. The read
command takes the user's input and stores it in a variable. In this case, the variable is called name
.
Once the user has entered their name, the script will get the first argument and store it in a variable called first_arg
. The first_arg
variable will contain the value of the first argument that was passed to the script.
Finally, the script will print the value of the name
and first_arg
variables back to the user.
When the script is run, the first argument that we pass to the script will be stored in the first_arg
variable. The script will then echo the value of the name
and first_arg
variables back to us.
Here is an example of what the output of the script might look like:
What is your name?
Pankaj
Your name is Pankaj.
The first argument is Pankaj
Example of If else in Shell Scripting by comparing 2 numbers
Sure, here's an example of how to use if-else statements in shell scripting to compare two numbers:
#!/bin/bash
num1=5
num2=10
if [[ $num1 -lt $num2 ]]; then
echo "Number 1 ($num1) is less than number 2 ($num2)"
else
echo "Number 1 ($num1) is greater than or equal to number 2 ($num2)"
fi
In this example, we first assign the values 5 and 10 to variables num1
and num2
, respectively. We then use the [[ ]]
syntax to compare the two numbers. The -lt
operator checks whether the first number is less than the second number.
If the condition is true (i.e., num1 is indeed less than num2), the echo
statement inside the if
block will be executed, printing the message "Number 1 ($num1) is less than number 2 ($num2)".
On the other hand, if the condition is false (i.e., num1 is greater than or equal to num2), the echo
statement inside the else
block will be executed, printing the message "Number 1 ($num1) is greater than or equal to number 2 ($num2)".
Double square brackets ([[ ]])
instead of single square brackets ([ ]])
) for comparison.
Thank you for reading until here. See you next time.