-
Notifications
You must be signed in to change notification settings - Fork 8
/
array-append.sh
executable file
·60 lines (48 loc) · 1.39 KB
/
array-append.sh
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
# Declare a string array
arrVar=("AC" "TV" "Mobile" "Fridge" "Oven" "Blender")
# Add new element at the end of the array
arrVar+=("Dish Washer")
echo ${arrVar[@]}
# split string to array
my_string="Ubuntu;Linux Mint;Debian;Arch;Fedora"
my_array=($(echo $my_string | tr ";" "\n"))
echo ${my_array[@]}": "${my_array[0]}", "${my_array[1]}
# file in array, read file into array
mapfile -t arrVar < my_file_with_lines.txt
# Iterate the loop to read and print each array element
for value in "${arrVar[@]}"; do
echo $value
done
# is element in array
# 1 - candidate
# 2 - array of elements
function is_in_array () {
local next_array_element search_string="$1"
shift
for next_array_element; do [[ "${next_array_element}" == "${search_string}" ]] && return 0; done
return 1
}
is_in_array "Mobile" ${arrVar[@]}
is_in_array "Mobile" "${arrVar[@]}"
# is_in_array_result="$?"
# if [[ $is_in_array_result > 0 ]]; then
if [[ "$?" > 0 ]]; then
echo " not in "
else
echo "Mobile"" in array"
fi
##########################################
folders=(one two three four)
reverse_array() {
local array=("$@")
local length=${#array[@]}
local reversed_array=()
for (( i=$length-1; i>=0; i-- )); do
reversed_array+=("${array[i]}")
done
echo "${reversed_array[@]}"
}
reversed=$(reverse_array "${folders[@]}")
for each_folder in ${reversed[@]}; do
echo $each_folder
done