Shell Yeah! Unleashing the Fun in Shell Scripting
Let's have a level understanding of what Shell scripting is and why we need it:
Imagine having a toy robot that can follow your commands. You can instruct it to walk forward, turn left or right, pick up things, and so on. Similarly, a computer is a smart robot that can do a lot of things but needs to be told what to do step-by-step.
Shell scripting is like creating a set of commands for the computer to follow, just like you do with your toy robot. Instead of physical actions, you give the computer instructions to do things like opening a program, copying files, moving folders, and more.
It's like having a magical helper that can do things for you while you sit back and relax. With shell scripting, you can tell the computer what to do, and it will do it for you!
Types of Shells:
Before we dive into writing shell scripts, it's important to understand the different types of shells that are available. The most common shell scripts are:
Bourne shell (sh)
Bash shell (bash)
Korn shell (ksh)
C shell (csh)
Z shell (ssh)
NOTE: Bash is the most widely used shell, and it is the default shell on most Linux distributions.
Writing a shell script:
To write a shell script, you can follow these basic steps:
Open a text editor such as Notepad, Sublime Text, or Vim.
Start your script with a "shebang" line that tells the system which shell to use. For example, if you want to use Bash, you would start your script with
#!/bin/bash.
Write your commands in the text editor. Each command should be on a separate line.
Save the file with a
.sh
extension, for example,myscript.sh
.
Make the file executable by running the
chmod
command, which changes the permissions of the file. For example, if your script is named"
myscript.sh
"
, you would use the following command to make it executable:chmod +x
myscript.sh
Run the script by entering
./
myscript.sh
in the terminal
Let's now look at what variables are in Shell Scripting:
In shell scripting, variables are used to store values that can be referenced and used later in the script. You can think of variables as containers that hold information, just like boxes that hold things in real life.
To declare a variable in shell scripting, you need to assign a value to it using the equal sign (=). For example:
greeting="Hello"
name="John"
Note: When declaring a variable, you do not include the ($)dollar sign. The dollar sign is only used when accessing the variable.
In this example, we have declared two variables: $greeting and $name. $greeting is assigned the value "Hello", and $name is assigned the value "John".
To access the value of a variable later in the script, you simply need to prefix its name with a dollar sign ($). For example:
echo $greeting $name
This will output the values of the $greeting and $name variables to the console.
Variables can hold many different types of data, such as strings, numbers, and even arrays. It's important to note that variables are case-sensitive in shell scripting, so $greeting and $Greeting would be two different variables.
Let's us now look at to Condition's:
Conditions are used to test whether a certain condition is true or false, and execute different commands based on the result. In shell scripting, you can use the if statement to test conditions. For example:
if [ $num -gt 10 ]
then
echo "The number is greater than 10."
else
echo "The number is less than or equal to 10."
fi
NOTE: see we have ended our if statement with a fi it's the opposite of if This script tests whether a variable $num is greater than 10 and outputs the appropriate message to the console.
Let's now look at operators.
Now, you must be wondering what "-gt"
is. These are basically operators that can be used in shell scripting. We all know about:
Arithmetic Operators
Comparison Operators
Logical Operators
But in shell scripting, "-gt"
and "-lt"
are comparison operators that stand for "greater than" and "less than", respectively. We have just assigned a special name to them. Here are a few more:
-eq:
This operator is used to check whether two values are equal. For example: [ $a -eq $b ]
-ne:
This operator is used to check whether two values are not equal. For example: [ $a -ne $b ]
-ge:
This operator is used to check whether the value on the left is greater than or equal to the value on the right. For example: [ $a -ge $b ]
-le:
This operator is used to check whether the value on the left is less than or equal to the value on the right. For example: [ $a -le $b]
Let's now look at loops:
Loops are used to execute a set of commands repeatedly until a certain condition is met. In shell scripting, you can use the "for" loop or the "while" loop to accomplish this. For example:
Here's an example of a simple "for" loop:
for i in {1..5}
do
echo $i
done
NOTE: Here we ended our "for" loop with "done".
This script will output the numbers 1 to 5 to the console.
Here's an example of a "while" loop:
num=1
while [ $num -le 5 ]
do
echo $num
num=$((num+1))
done
This script will also output the numbers 1 to 5 to the console, but it uses a "while" loop instead.
This script will also output the numbers 1 to 5 to the console, but it uses a "while" loop instead.
Overall, variables, conditions, and loops are essential elements of shell scripting that allow you to create more complex and powerful scripts.
And here's the end of our small Shell Scripting blog. But remember, with great power comes great shell scripting! So go forth, write some amazing scripts, and make your computer do all the boring work for you!