Skip to content

Commit

Permalink
Update loops.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
lorcai authored Sep 3, 2024
1 parent 36d2e9f commit ad99b8b
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion loops.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Just stock loops
##############################################################
https://stackoverflow.com/questions/8880603/loop-through-an-array-of-strings-in-bash

# 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
Expand Down Expand Up @@ -33,3 +44,32 @@ 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]}
#
##############################################################





0 comments on commit ad99b8b

Please sign in to comment.