What is Shell Scripting?
Shell scripting consists of a set of commands that is used to perform a task. All the commands run sequentially.
It is like a list of tasks that instruct our operating system to follow and the tasks include managing files, running programs, and automating repetitive tasks.
It is useful in saving time and reducing human error by automating repetitive tasks.
What is the use of Shell Scripting?
It is used for automating repetitive system tasks such as file backups, user account management, and monitoring of system resources. It helps in saving time, increasing accuracy, and simplifying complex tasks.
How does shell scripting benefit DevOps engineers?
Shell scripting can benefit DevOps engineers in tasks such as automation, configuration management, and troubleshooting.
Some terms used in Shell Scripting:
What is Shell?
Shell is an environment where we run Linux commands.
It is of two types Command line Shell and Graphical Shell
Command Line shell: It is also known as the command prompt or the terminal where the user controls their system by using textual commands.
Graphical shell: It is a user interface that provides a graphical representation of the operating system where users interact with their keyboard and mouse.
Some of the common types of shells are:
Bourne shell
C shell
Bash shell
Korn shell
Z shell
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:
-> bash script.sh
or by ./script.sh
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 simply typing which bash
command.
Terminal:
-> It is a program that establishes a connection with the server.
-> It is used for accessing the shell
What is #!/bin/bash?
Can we write #!/bin/sh
as well?
#!/bin/bash
or #!/bin/sh
in script file is known as a shebang line or hashbang line. It is used to write on top of the script file.
#!/bin/bash
: It is used for enabling bash shell as an interpreter to execute the script.
#!/bin/sh
: It is used for the default shell of the system to execute the script which might be different shells like Bourne, and Korn shells. In maximum systems by default bash shell is used.
There are some hands-on practice questions related to this topic.
Q1. Write a Shell Script that prints I will complete #90DaysOofDevOps 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.
We can also view this script using another command:
By using this command first, we have to give all permission to the user to use ./script.sh
the command. ./
is used for the execution of files.
chmod 777 script.sh
to allow all the permissions after that it will run.
Q2. Write a Shell Script to take user input, input from arguments, and print the variables
-> Here read will take input from the user. ${you} will read the value which the user provides.
-> Here it will print the input provided by the user.
-> $1 will be our first argument which is written after our script file name.
-> Here it will print the first argument "Riya" .
-> ${name} will take the variable input.
-> Here it prints the variable.
Q3. Write an Example of If else in Shell Scripting by comparing 2 numbers
-> It will check if the input argument is odd or even
-> It shows the value is even or odd according to the argument.
These are some basic concepts related to the Linux Shell Scripting which we used in DevOps.We will see the advanced command of shell scripting in our next blog.