array Demos

This commit is contained in:
2025-09-07 14:15:56 +02:00
parent 96f5040dfe
commit 0d67903acf
8 changed files with 137 additions and 0 deletions

16
arrays/access.sh Executable file
View File

@@ -0,0 +1,16 @@
#! /usr/bin/bash
# Array of numbers
fib=(0 1 2 3 5 8)
#Array of Strings
family=("Marge","Homer","Bart","Lisa","Maggie")
# outputs only element 0
echo "fib=${fib}"
# output the whole array
echo "fib=${fib[@]}"
#output element by index
echo "fib[4]=${fib[4]}"

10
arrays/associative_array.sh Executable file
View File

@@ -0,0 +1,10 @@
#! /usr/bin/bash
declare -A family
family=([father]="Homer" [mother]="Marge" [son]="Bart" [daughter]="Lisa" [baby]="Maggie")
echo "Iterating through associative array using while loop:"
while IFS= read -r key && IFS= read -r value <&3; do
echo "Key: $key, Value: $value"
done < <(printf '%s\n' "${!family[@]}") 3< <(printf '%s\n' "${family[@]}")

20
arrays/define.sh Executable file
View File

@@ -0,0 +1,20 @@
#! /usr/bin/bash
a1=(0 1 2 3 4)
family=("Marge" "Homer" "Bart" "Lisa" "Maggie")
echo "a1=${a1[@]}"
echo "family=${family[@]}"
echo "Länge family: ${#family[@]}"
# Define an Array with declare -a
declare -a sbc
sbc[0]="Raspberry Pi"
echo "Länge sbc: ${#sbc[@]}"
echo "sbc=${sbc[@]}"
sbc[1]="Orange Pi"
echo "Länge sbc: ${#sbc[@]}"
echo "sbc=${sbc[@]}"
sbc[2]="Banana Pi"
echo "Länge sbc: ${#sbc[@]}"
echo "sbc=${sbc[@]}"

39
arrays/iterate.sh Executable file
View File

@@ -0,0 +1,39 @@
#! /usr/bin/bash
# Array of numbers
fib=(0 1 2 3 5 8)
#Array of Strings
family=("Marge" "Homer" "Bart" "Lisa" "Maggie")
# Einfache for Schleife
for name in "${family[@]}"
do
echo "for: name=${name}"
done
echo "---"
# C-ähnliche Schleife im Index
len=${#family[@]}
for ((i=0;i < len; i++));do
echo "for-i: name=${family[i]},i= ${i}"
done
echo "---"
# while Schleife
counter=0
while [ ${counter} -lt ${len} ]; do
echo "while: name=${family[counter]},counter=${counter}"
((counter++))
done
echo "---"
# until Schleife
counter=0
until [ $counter -ge $len ]; do
echo "until: ${family[counter]}"
((counter++))
done

8
arrays/length.sh Executable file
View File

@@ -0,0 +1,8 @@
#! /usr/bin/bash
a1=(0 1 2 3 4 5)
family=("Marge" "Homer" "Bart" "Lisa" "Maggie")
echo "Größe family: ${#family[@]}"
echo "Größe a1: ${#a1[@]}"

16
arrays/merge.sh Executable file
View File

@@ -0,0 +1,16 @@
#! /usr/bin/bash
family=("Marge" "Homer" "Bart" "Lisa" "Maggie")
neighbours=("Ned" "Maude" "Todd" "Rod")
echo "family=${family[@]}"
echo "Länge family: ${#family[@]}"
echo "neighbours=${neighbours[@]}"
echo "Länge neighbours: ${#neigbours[@]}"
merged=(${family[@]} ${neighbours[@]})
echo "merged=${merged[@]}"
echo "Länge merged: ${#merged[@]}"

6
arrays/slice.sh Executable file
View File

@@ -0,0 +1,6 @@
#! /usr/bin/bash
family=("Marge" "Homer" "Bart" "Lisa" "Maggie")
echo "Kinder: ${family[@]:2:4}"
echo "Eltern: ${family[@]:0:2}"

22
arrays/sort.sh Executable file
View File

@@ -0,0 +1,22 @@
#! /usr/bin/bash
family=("Marge" "Homer" "Bart" "Lisa" "Maggie")
neighbours=("Ned" "Maude" "Todd" "Rod")
echo "family=${family[@]}"
echo "Länge family: ${#family[@]}"
echo "neighbours=${neighbours[@]}"
echo "Länge neighbours: ${#neigbours[@]}"
merged=(${family[@]} ${neighbours[@]})
echo "merged=${merged[@]}"
echo "Länge merged: ${#merged[@]}"
# sorting the merged array
sorted=($(printf "%s\n" "${merged[@]}" | sort -n))
echo "sorted=${sorted[@]}"