I Am Sharing 'FC - MOD3 - Shell - Scripting-1' With You
I Am Sharing 'FC - MOD3 - Shell - Scripting-1' With You
Example2: greet() {
echo "Hello, $1!"
}
greet "Alice"
greet "Bob"
Variable Scopes
■ Every declared variable must have a scope, defining where in the program the variable
can be used.
■ Variable scopes in Bash can be defined in two ways
1. Global variable
2. Local variable:
➢ Bash Global Variables
■ Variables declared in a shell script are referred to as global variables.Global variables
can be accessed within a function or any nested blocks of a shell script file.
■ The default variable declared in a script file is called a global variable.
setAge() {
echo "Inside Function Age: $AGE"
}
AGE=40
setAge
echo "Script Age: $AGE“
■ Output
Inside Function Age: 40
Script Age: 40
Bash Local variables
■ Local variables are declared inside a block of code or a function. The scope of these
variables is visible only within the block where they are declared.
■ Syntax:
– local variablename=variablevalue
Eg: setAge() {
local AGE=25
echo "Local Variable Age: $AGE"
}
AGE=40
setAge
echo "Global Age: $AGE“
Output
Local Variable Age: 25
Global Age: 40
Operators
■ Operator is a symbol in programming that performs an operation on operands.The
operators are:
1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) String Operators
5) File Test Operators
Arithmetic Operators
ADD TWO NUMBERS:
#! /bin/bash
# Calculate the sum of two integers with pre initialize values
# in a shell script
# Calculate sum
sum=$(( $a + $b ))
Output
Relational Operators
Logical Operators
■ These operators are used to perform logical operations on variables/expressions/data.
➢ Logical AND (&&): This is a binary operator, which returns true if both the operands are
true otherwise returns false.
➢ Logical OR (||): This is a binary operator, which returns true if either of the operands is
true or if both the operands are true. It returns false only if both operands are false.
➢ Not Equal to (!): This is a unary operator which returns true if the operand is false and
returns false if the operand is true.
String Comparison Operators
Operator Description
== Returns true if the strings are equal
!= Returns true if the strings are not equal
-n Returns true if the string to be tested is
not null
-z Returns true if the string to be tested is
null
File Test Operators
Decision Making
■ Two types of decision-making statements are used within shell scripting.
■ They are: 1) if-else statement: if else statement is a conditional statement. It can be used to
execute two different codes based on whether the given condition is satisfied or not.
■ Different types are:
1) if-fi
2) if-else-fi
3) if-elif-else-fi
4) nested if-else
Syntax for If Conditional Statements:
if [ expression ]; then
statements
fi
e.g.:
If-Else Conditional Statements
■ The if-else conditional statements in Bash allow you to execute different code blocks depending on whether a
condition is true or false.
■ Syntax:
if [ condition ]; then
# Execute code block if the condition is true
else
# Execute code block if the condition is false
fi
Eg: #! /bin/sh
age = 25
if [ $age -gt 60 ]; then
echo "Senior Citizen"
else
echo "Not Senior Citizen"
fi
If..Elif..Else Statements
➢ Use if..elif..else conditional statements in Bash to execute different code blocks based on
multiple conditions.
➢ Syntax:
if [ condition1 ]; then
# Execute code if condition1 is true
elif [ condition2 ]; then
# Execute code if condition1 is false and condition2 is true
else
# Execute code if both condition1 and condition2 are false
fi
Example
age=25
if [ $age -gt 60 ]; then
echo "Senior Citizen"
elif [ $age -lt 14 ]; then
echo "Child"
else
echo "Adult"
fi
2) case-esac statement
• case-sac is basically working the same as switch statement in programming. Sometimes if we have to check
multiple conditions, then it may get complicated using if statements.
• Syntax:
case Expression in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
*) # Default case ;;
esac
Eg: Name="Shivanika"
case “$Name” in
#case 1
"Anika") echo "Profession: Software Engineer" ;;
#case 2
"Arnav") echo "Profession: Doctor" ;;
#case 3
"Shivanika") echo "Profession: Lawyer" ;;
esac
Loops
■ Loops are used to execute code block for a number of times.
■ for loop
■ Syntax:
for element in list; do
# Code block
done
❑ Example:
for i in 1 2 3 4 5; do
echo "Number: $i"
done
while loop
■ The while loop in Bash allows for the repeated execution of code as long as a specified
condition is true. If the condition becomes false, the loop exits.
■ Syntax:
while [ condition ]; do
# code block
done
❑ Example:
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
for index loop
■ for index loop is similar to C language for index loop It executes code multiple times based on condition
is true, It starts with initial value and iteration contails the value to be incremented by 1.
■ Syntax:
for (( assignment; condition; iteration )); do
# code block
done
❑ Example:
for (( i=0; i<5; i++ ));do
echo $i
done
Until loop
■ The until keyword in Bash is employed to repeatedly execute code until a specified condition
becomes true, at which point the loop exits.
■ Syntax:
until [ condition ]; do
# code block
done
❑ Example:
i=0
until [ i -eq 100 ];
do
echo "$i"
i=$(( i+1 ))
done
Writing Bash Scripts
■ To write a Bash Script, we should follow these steps:
• First, we should create a file with the .sh extension.
To create and write a file with the .sh extension we can use gedit text editor.
Syntax: gedit/nano scriptname.sh
• Next, we can write down the bash scripts within it.
The first line of the script file should be:
#! /bin/bash
This will tell the system to use Bash for execution.
Then we can write our own scripts.
■ After that, we can provide execution permission to it, after saving the file.
o To provide execution permission,
chmod +x scriptname.sh
o To execute the script, we can use:
./scriptname.sh
A basic Bash script is just a series of commands stored in a text file,which can be executed
in a terminal.
Simple bash script
#! /bin/bash
#This is a simple Bash script
echo "Hello, $USER!" # Display a greeting message