In order to get things done in our daily lives, we must perform repetitive tasks. For instance: when running a number of commands, request regular backups from administrators, and keep an eye on the systems.
It will take a great deal of time and effort to repeat these tasks frequently.
As a result, shell scripting entered the picture. Shells can execute all of these commands directly from a stored file rather than having to type them out each time, which makes our task automated and simple and eliminates the need for us to perform the same tasks repeatedly.
In today's blog, I’m going to explain shell scripting and how to write Scripts to perform daily tasks.
One question that will be on everyone’s mind is: what is the difference between the program and the scripts?
The script is the single file in which we write the commands, eg: mkdir, touch, echo, so in the scripting, we write the commands in the script’s files.
Wherever in the programming, we write the functions, and object-oriented programs in order to build the application.
What is Shell?
A shell is a user program that provides an interface for users to use operating system services. Shell accepts human-readable commands from a user and converts them into something that the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.
What is Linux Shell Scripting?
A shell script is a computer program designed to be run by a Linux shell, a command-line interpreter. The various dialects of shell script are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.
To check which shell we are using the command is which bash
which outputs the shell our system currently using.
Let's write our first shell script
So let's open a file using the vim editor
Vim
firstScript.sh
The first line in the script will be declaring which bash we are going to use. So in my system, it was using the bin/bash
#!/bin/bash
Declaring that we are writing the bin/bash script
Now, we can perform multiple commands by writing the commands in the script file.
To run our shell script, we need to write bash <filename>
as in my case bash firstScript.sh This command will run our script and perform the tasks written in it.
Now, to make it an executable script, we need to write ./
in front of it as in Linux we use ./
to execute an executable file.
Before that, we need to give the file permission as an executable file using the chmod
command.
chmod 777
firstScript.sh
Now our file has the read-write permission it is ready to run as an executable file.
./<filename>
in my case ./
firstScript.sh
Variables & arguments in a bash shell script
$
symbol is used for variables in bash scripts. To create a variable variable_name=value
Eg: name:"Ritik"
output:
User input and arguments
User input
Let's say we entered our name in the variable and we need the user to enter the age
The command for user input is
read
.read <testvariable>
the user input will get stored in the testvariable.User Argument
The order in which arguments are sent to a script determines the order in which they are processed. The first argument can be accessed inside the script using $1. The indexing of the arguments starts at one. In a similar way, $2 can be used to access the second argument, and so on. This representation of the arguments based on their positions is referred to as the positional parameter.
echo "Username: $1"; echo "Age: $2"; echo "Full Name: $3";
output
If-else conditions
The syntax for if else in shell scripting is
if [condition] then statement1 else statement2 fi
There are four keywords in this statement: if, then, else, and fi.
There is a condition after the word "if." Which indicates finish
To determine which statement the processor will execute, this condition is assessed.
The processor will execute the statement(s) following the keyword then if the condition evaluates to TRUE. It is referred to as statement 1 in the syntax.
The statement(s) followed by the keyword else will be executed if the condition evaluates to FALSE. In the function syntax, this is referred to as statement 2.
***Note****: Shell scripting is case-sensitive, just like C programming, so this is something to keep in mind. As a result, you must use caution when using keywords in your code.*
Example of If else in Shell Scripting by comparing 2 numbers.
#!/bin/bash
If [ “$1” == “$2” ]
then
echo “both are the same arguments”
else
echo “both are the different arguments”
fi
output:
Thanks for reading! I hope you understood these concepts and learned something.
If you have any queries, feel free to reach out to me on LinkedIn.