Skip to content

Commit e3174e4

Browse files
committed
Added array helper function
Added function which checks if element in array (bash)
1 parent 55ba6f1 commit e3174e4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

root/app/lib/utils

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,33 @@ function yn() {
1616
else
1717
echo '0'
1818
fi
19+
}
20+
21+
#
22+
# Checks if item is in array
23+
# @param $1 Array as "${array[@]}"
24+
# @param $2 String we are looking for
25+
# @return 0 If not in array, 1 if in array
26+
#
27+
# arrayContains array "element"
28+
# echo $?
29+
#
30+
function arrayContains() {
31+
# Check if empty
32+
if [ ! -n "$1" ]; then
33+
return 0
34+
fi
35+
36+
local array="$1[@]"
37+
# Check array for element
38+
for element in "${!array}"; do
39+
40+
# Check if we found element in array
41+
if [ "$2" = "$element" ]; then
42+
return 1
43+
fi
44+
done
45+
46+
# Element not found
47+
return 0
1948
}

0 commit comments

Comments
 (0)