-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow_control.bash
More file actions
76 lines (63 loc) · 1015 Bytes
/
flow_control.bash
File metadata and controls
76 lines (63 loc) · 1015 Bytes
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
#!/bin/bash
# Using if, elif, else
x=25
if [ $x -eq 25 ]; then
echo "The value of x is 25"
elif [ $x -gt 25 ]; then
echo "The value of x is bigger than 25"
else
echo "The value of x is lower than 25"
fi
# Loops For while and until
# for
for i in {1..5}; do
echo "Iteration $i"
for(( i=0; i<=10; i+=2))
do
echo $i
done
# while
count=1
while [ $count -le 5 ]; do
echo "Count is $count"
((count++))
done
# until
until [ $count -gt 5 ]; do
echo "Count is $count"
((++count))
done
# Loops control break and countinue
# Using break
for i in {1..5}; do
if [ $i -eq 3 ]; then
break
fi
echo "Iteration $i"
done
# Using continue
for i in {1..5}; do
if [ $i -eq 3 ]; then
continue
fi
echo "Iteration $i"
done
# Functions
# Defining a function
greet() {
echo "Hello, $1"
}
# Using the function
greet "Alice"
add() {
local sum=$(( $1 + $2 ))
echo $sum
}
result = $(add 5 3)
echo "Sum is $result"
multiply() {
local product=$(( $1 + $2 ))
return $product
}
multiply 4 5
echo "Product is $?"