-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathARRAY_IN_SHELL_SCRIPT
More file actions
53 lines (43 loc) · 1.31 KB
/
ARRAY_IN_SHELL_SCRIPT
File metadata and controls
53 lines (43 loc) · 1.31 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
https://clubmate.fi/associative-arrays-in-bash/
https://lzone.de/cheat-sheet/Bash%20Associative%20Array'
https://stackoverflow.com/questions/40387649/reading-a-file-into-an-associative-array-in-bash
http://www.cs.umsl.edu/~sanjiv/classes/cs2750/lectures/kshfns.pdf
https://www.geeksforgeeks.org/array-basics-shell-scripting-set-1/
https://riptutorial.com/bash/example/19457/read-lines-of-a-file-into-an-array
https://peniwize.wordpress.com/2011/04/09/how-to-read-all-lines-of-a-file-into-a-bash-array/
https://kaijento.github.io/2017/03/19/bash-read-file-into-array/
https://canvas.bham.ac.uk/courses/17217/pages/read-file-into-bash-array
#!/bin/bash
declare -A array=()
IFS=':'
while read key value
do
array[$key]=$value
done < file.txt
for key in ${!array[@]}
do
echo $key:${array[$key]}
done
unset IFS
#!/bin/bash
declare -A address
while IFS=':' read name add
do
address[$name]=$add
#echo "$name - ${address[$name]}"
done < file.txt
for name in "${!address[@]}"
do
echo "$name:${address[$name]}"
done
=======================================================
#!/bin/bash
#Declare array with 4 elements
ARRAY=( 'Debian Linux' 'Redhat Linux' Ubuntu Linux )
# get number of elements in the array
ELEMENTS=${#ARRAY[@]}
# echo each element in array
# for loop
for (( i=0;i<$ELEMENTS;i++)); do
echo ${ARRAY[${i}]}
done