-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearning-bash.sh
More file actions
executable file
·108 lines (87 loc) · 1.52 KB
/
learning-bash.sh
File metadata and controls
executable file
·108 lines (87 loc) · 1.52 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
## 1. LOOPS
# WHILE LOOP
counter=1
# -le means "less than or equal to"
while [ $counter -le 10 ]
do
echo "$counter"
((counter++))
done
echo "All done with while loop"
# FOR LOOP
names="Diego Jonas Gallardo"
for name in $names
do
echo $name
done
echo "All done with for loop"
# RANGE LOOP
for value in {1..5}
do
echo $value
done
echo "All done with range loop"
## 2. VARIABLES
# Setting and Reading
variable="variable"
echo "I am reading a $variable"
# Setting local variables
# try uncommenting the global $sum and running again
calculate_sum(){
local a=5
local b=5
local sum=$((a + b))
}
calculate_sum
if [ -z $sum ]; then
echo "Cannot find value of \$sum because it is a local variable"
else
echo "$sum"
fi
## 3. CONDITIONALS
number=10
if [ $number -le 20 ]; then
echo "Number is less than"
fi
logged_in=false
if [ $logged_in ]; then
echo "User is logged in"
else
echo "User is not logged in"
fi
age=26
if [ $age -lt 25 ]; then
echo "User is less than 25"
elif [ $age -eq 25 ]; then
echo "User is 25"
else
echo "User older than 25"
fi
## 3. FUNCTIONS
# First way to Declare a Function
add_numbers() {
a="$1"
b="$2"
c=$(($1 + $2))
echo "The sum of $a and $b is $c"
}
# notice how args are passed
add_numbers 4 6
# Second way to Declare a Function
function my_name() {
first="$1"
last="$2"
echo "Your full name is $first $last"
}
my_name Diego Gallardo
success_function() {
return 0
}
success_function
echo "$?"
failure_function() {
return 1
}
failure_function
echo "$?"