-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path6-Arrays.sh
More file actions
60 lines (46 loc) · 1.42 KB
/
6-Arrays.sh
File metadata and controls
60 lines (46 loc) · 1.42 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
#!/bin/bash
#Name: Bash Tutorial 6 Arrays
#Description: Tutorial shows how to declare and use Numeric and Associative Arrays
#Video: https://youtu.be/FBv7aEn2Cik
#Global array
declare -A AssociativeArray #Array index is in named form
declare -a NumericArray #Array index is in numeric form
#Local array
function Foo() {
local -a LocalArray #Numeric array which only exists in this function
#Set a specific point in the array for each value
LocalArray[0]="Hi"
LocalArray[1]="There"
LocalArray[9]=15 #No need to be consecutive or a string
#Read specific values
echo "${LocalArray[0]} ${LocalArray[1]}" #Hi There
#Read entire array
echo "${LocalArray[@]}" #Hi There 15
}
#Call function Foo
Foo
echo "Local Array ${LocalArray[@]}" #Nothing, as the array doesn't exist outside of Function Foo
echo
#Set values in Associative Array
AssociativeArray[Var1]="Var1"
AssociativeArray[Var2]="Var2"
#Check if values exist in Associative Array
if [ "${AssociativeArray[Var1]}" ]; then
echo "Var1 exists"
else
echo "Var1 does not exist"
fi
if [ "${AssociativeArray[Var3]}" ]; then
echo "Var3 exists"
else
echo "Var3 does not exist"
fi
#Add string into Numeric Array
NumericArray+=("All work")
NumericArray+=("and no play")
NumericArray+=("makes Jack a dull boy")
echo "Writing NumericArray to File /tmp/test.txt"
printf "%s\n" "${NumericArray[@]}" > /tmp/test.txt
echo
echo "Reading File /tmp/test.txt"
cat /tmp/test.txt