-
Notifications
You must be signed in to change notification settings - Fork 1
/
09_if_Statement.sh
executable file
·87 lines (70 loc) · 2.01 KB
/
09_if_Statement.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env bash
# Control Structures in Bash
# 1. Control Structure allows us to control how script execution happens.
# 2. Run Specified code Based on condition.
# one of the control structure is if statement.
# if statements execute code based on a conditional expression.
# if ....
# then
# ....
# fi
# Syntax :-
# if condition
# then
# script
# fi
# we can also use if else statement
# if else allows use to execute else statement if condition of if is false.
# Syntax :-
# if condition
# then
# script
# else
# script
# fi
# The expression is a test of some kind. Often, the extended test with two bracket or an arithmetic evalution.
# It can also be a command because command always finish with zero and non-zero status interpreted as true or false.
#
# Example :-
echo "if else example 1 :-"
echo
declare -i a=3
if [[ $a -gt 4 ]]
then
echo "$a is greater then 4."
else
echo "$a is not greater then 4."
fi
echo
# we can also use arithmetic evalution bu using circular brackets
# Example :-
echo "if else example 2 with arithmetic evalution :-"
echo
echo
declare -i x=9
if (( $x > 4 ))
then
echo "$x is greater then 4."
else
echo "$x is not greater then 4."
fi
echo
# we cal also use else if statement in BASH using elif keyword
# in this bash first check if statement if it is not true
# it goes to elif statement if elif statement is true then it execute the elif statement script
#otherwise it goes to else statement and execute else statement script
# Example :-
echo "if elif statement example :-"
echo
echo
declare -i y=5
if (( $y > 6 ))
then
echo "$x is greater then 4."
elif (( $y > 3 ))
then
echo "$y is greater then 3"
else
echo "$y is not greater then 4."
fi
echo