-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops.sh
75 lines (53 loc) · 1.67 KB
/
loops.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
60
61
62
63
64
65
66
67
68
69
70
# Just stock loops
##############################################################
# Create sample list to be iterated
# <manual>
# Delete empty lines just in case
sed -i '/^$/d' samplelist.txt
# Read into array
readarray -t samplelist < samplelist.txt
##############################################################
# Reference: https://stackoverflow.com/questions/8880603/loop-through-an-array-of-strings-in-bash
# Basic
## declare an array variable
declare -a arr=("element1" "element2" "element3")
## now loop through the above array
for i in "${arr[@]}"
do
echo "$i"
echo ${arr[i]}
echo "${arr[@]}"
# or do whatever with individual element of the array
done
# You can access them using echo "${arr[0]}", "${arr[1]}" also
##############################################################
# Loop with counter
#!/bin/bash
## declare an array variable
declare -a array=("one" "two" "three")
# get length of an array
arraylength=${#array[@]}
# use for loop to read all values and indexes
for (( i=0; i<${arraylength}; i++ ));
do
echo "index: $i, value: ${array[$i]}"
done
##############################################################
# Use
# Loop with counter
#!/bin/bash
## declare an array variable
readarray -t samplelist < samplelist.txt
# get length of an array
arraylength=${#samplelist[@]}
# use for loop to read all values and indexes
for (( i=0; i<${#samplelist[@]}; i++ ));
do
echo "index: $i, value: ${samplelist[$i]}"
done
# Note:
# Length of array: ${#samplelist[@]}
# Lenght of string of the element 1: ${#samplelist[1]}
# Value of the element 1: ${samplelist[1]}
#
##############################################################