diff --git a/arrays/.gitignore b/arrays/.gitignore new file mode 100644 index 0000000..b05de43 --- /dev/null +++ b/arrays/.gitignore @@ -0,0 +1,2 @@ +*.bak + diff --git a/arrays/access.sh b/arrays/access.sh new file mode 100755 index 0000000..4be343b --- /dev/null +++ b/arrays/access.sh @@ -0,0 +1,17 @@ +#! /usr/bin/bash +# File: access.sh + +# 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]}" + diff --git a/arrays/append.sh b/arrays/append.sh new file mode 100755 index 0000000..121c64a --- /dev/null +++ b/arrays/append.sh @@ -0,0 +1,11 @@ +#! /usr/bin/bash + +family=("Marge" "Homer" "Bart" "Lisa" "Maggie") + +echo "Länge family,vorher: ${#family[@]}" + +family+=(Herb) # Element anfügen. + +echo "family=${family[@]}" +echo "Länge family,nachher: ${#family[@]}" + diff --git a/arrays/associative_array.sh b/arrays/associative_array.sh new file mode 100755 index 0000000..5fa6c4d --- /dev/null +++ b/arrays/associative_array.sh @@ -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[@]}") diff --git a/arrays/create.sh b/arrays/create.sh new file mode 100755 index 0000000..cf97cbb --- /dev/null +++ b/arrays/create.sh @@ -0,0 +1,10 @@ +#! /usr/bin/bash +# File: create.sh + +a1=(0 1 2 3 4) +family=("Marge" "Homer" "Bart" "Lisa" "Maggie") + +echo "a1=${a1[@]}" +echo "family=${family[@]}" +echo "Länge family: ${#family[@]}" + diff --git a/arrays/declare.sh b/arrays/declare.sh new file mode 100755 index 0000000..f777ff0 --- /dev/null +++ b/arrays/declare.sh @@ -0,0 +1,14 @@ +#! /usr/bin/bash +# File: declare.sh + +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[@]}" + diff --git a/arrays/iterate.sh b/arrays/iterate.sh new file mode 100755 index 0000000..ba9f118 --- /dev/null +++ b/arrays/iterate.sh @@ -0,0 +1,41 @@ +#! /usr/bin/bash +# File: iterate.sh + +# 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 diff --git a/arrays/length.sh b/arrays/length.sh new file mode 100755 index 0000000..e3610db --- /dev/null +++ b/arrays/length.sh @@ -0,0 +1,9 @@ +#! /usr/bin/bash +# File: length.sh + +a1=(0 1 2 3 4 5) +family=("Marge" "Homer" "Bart" "Lisa" "Maggie") + +echo "Größe family: ${#family[@]}" +echo "Größe a1: ${#a1[@]}" + diff --git a/arrays/merge.sh b/arrays/merge.sh new file mode 100755 index 0000000..68086ee --- /dev/null +++ b/arrays/merge.sh @@ -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[@]}" diff --git a/arrays/power.sh b/arrays/power.sh new file mode 100755 index 0000000..ed93147 --- /dev/null +++ b/arrays/power.sh @@ -0,0 +1,34 @@ +#! /usr/bin/bash +# Datei: power.sh + +declare -A power + +# 1. Zeile (Orange Pi Zero 2W), x-Index: 0 + +power[0,0]="Orange Pi Zero 2W" +power[0,1]="1,014" +power[0,2]="1,99" + +# 2. Zeile (Banana Pi BPI-F3) x-Index: 1 +power[1,0]="Banana Pi BPI-F3" +power[1,1]="3,6" +power[1,2]="6,8" + +# 3. Zeile (Raspberry Pi 16GB), x-Index: 2 +power[2,0]="Raspberry Pi 16GB" +power[2,1]="2,25" +power[2,2]="8,0" + +# 4. Zeile (Radxa Zero), x-Index: 3 +power[3,0]="Radxa Zero" +power[3,1]="-" +power[3,2]="-" +# 2. Spalte: Stromaufnahme Leerlauf + +printf "%-25s %-10s %-10s\n" "SBC" "idle" "load" + +for ((x=0; x<4; x++)) +do + printf "%-20s %-10s %-10s\n" "${power[${x},0]}" "${power[${x},1]}W" "${power[${x},2]}W" +done + diff --git a/arrays/remove.sh b/arrays/remove.sh new file mode 100755 index 0000000..86de9ee --- /dev/null +++ b/arrays/remove.sh @@ -0,0 +1,11 @@ +#! /usr/bin/bash + +family=("Marge" "Homer" "Bart" "Lisa" "Maggie") + +echo "Länge family,vorher: ${#family[@]}" + +unset family[2] + +echo "family=${family[@]}" +echo "Länge family,nachher: ${#family[@]}" + diff --git a/arrays/slice.sh b/arrays/slice.sh new file mode 100755 index 0000000..7b71bdc --- /dev/null +++ b/arrays/slice.sh @@ -0,0 +1,6 @@ +#! /usr/bin/bash + +family=("Marge" "Homer" "Bart" "Lisa" "Maggie") + +echo "Kinder: ${family[@]:2:4}" +echo "Eltern: ${family[@]:0:2}" diff --git a/arrays/sort.sh b/arrays/sort.sh new file mode 100755 index 0000000..35ce4b0 --- /dev/null +++ b/arrays/sort.sh @@ -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[@]}"