Bash Scripting is one of the skills that any system adminsitrator or DBA’s should knows, why ? because it’s making your life much easier, for example, imagine you are about to change the persimisson of files for example under one directory, and inside it you have 10 files (i know it’s that much), Will you do it one by one, it will be a good idea, maybe you are fast typer but what if it will be more than 10 ?
Shell Scripting is the solution for this.
Bash is comamnd language and it’s widely available on various operating system, the name it’s self came from Bourne-Again SHell
Shell which is allows you for interactive or non-interactive execution.
Finally; which is scripting, the commands that will be executed one by one.
One of the simplest example which is “Hello World”, Save it as hello.sh
#!/bin/sh
echo “Hello, World”
if we need to modify this script to read name of the user; it will be like this :-
#!/bin/sh
echo “What is your name?”
read MY_NAME
echo “Hello, $MY_NAME”
Mathamtics examples :-
Shell Scripting can be used also in mathamtics operations such as the below:-
#!/bin/bash
((sum=25+35))
echo $sum
#!/bin/bash
((area =2*2))
echo $area
Looping :-
If you want to repeat something, then you should use either for or while which will make your life easier:-
The following example, will print the counter number each time, as you see the counter start from 5 and it will out once it will be 0
#!/bin/bash
for (( counter=5; counter>0; counter– ))
do
echo -n “$counter ”
done
printf “\n”
Using While but once it will be 5 the loop will be terminated.
#!/bin/bash
Bol_value=true
count=1
while [ $Bol_value ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++))
done
Operator | Description |
-eq | Checks if the value of two operands are equal or not; if yes, then the condition becomes true. |
-ne | Checks if the value of two operands are equal or not; if values are not equal, then the condition becomes true. |
-gt | Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. |
-lt | Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. |
-ge | Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true |
le | Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true.
|
If you want to learn more from here
Summary:-
- Shell is a program which interprets user commands through CLI like Terminal.
- Shell scripting is writing a series of command to execute.
- Shell scripting can help you create complex programs
Cheers
Osama