ThePretender73
ThePretender73

Reputation: 35

Adding into array (bash)

I'm currently trying to simulate a simple Version of a Buddy algorithm. This code extraction should simulate the process of adding a process into a array List in which $speicher = the RAM which should be formatted. (The variables are german) $prozess = the process which is added into the RAM. When trying to run the script i get the error = to much recursion in line 19 =arraySpeicher+=($j/2). (again the error is in german so im not entirely sure if thats the correct error) $i should have the value of $prozess which needs to be formatted to fit the Buddy algorithm principal.

How do i fix the Problem of wanting to add the formatted values into my array?

#!/bin/bash
echo "Wie groß ist ihr Arbeitsspeicher?: " 
read speicher
#Einlesen des Arbeitsspeichers
echo "Wie groß ist ihr Prozess den sie dem Speicher hinzufügen wollen?: "
read prozess

if (( prozess < 0 )); then
   echo "Dein Prozess ist zu klein & hat keine relevante Größe." 
   exit
   fi

arraySpeicher=($speicher)
arrayName=()

for i in "${arraySpeicher[@]}"; do
    if (( $prozess <= $i )); then
        j=$i
        unset arraySpeicher[$i]
        while (( $prozess < $j/2 )); do
            arraySpeicher+=($j/2)
            ((j=j/2))
            done
        arraySpeicher+="p ${#ArrayName[@]}"
        arrayName+=$prozess

    fi
    done

echo "Dies ist ihr Speicher ${arraySpeicher[@]} nach hinzufügen des Prozesses."

while (( true )); do
    PS3="Möchten sie einen Prozess hinzufügen / entfernen / aufhören?"
    select eingabe in hinzfügen entfernen aufhören 
    do 

if (( $eingabe == hinzufügen )); then
    echo "wie groß ist der Prozess?:"
    read prozess

    if (( prozess < 0 )); then
        echo "Ihr Prozess ist zu klein." 
        exit
        fi

    for i in "${arraySpeicher[@]}"; do
        if (( $i == int )) #TODO: nur zahlen dürfen behandelt werden, prozesse nicht!!
        if (( $prozess <= $i )); then
            j=$i
            unset arraySpeicher[$i]
            while (( $prozess < $j/2 )); do
                arraySpeicher+=($j/2)
                ((j=j/2))
                done
            arraySpeicher+="p ${#ArrayName[@]}"
            arrayName+=$prozess

            break
        fi
        echo "Dies ist ihr Speicher ${arraySpeicher[@]} nach hinzufügen des Prozesses."
        fi
        done    


if (( $eingabe == entfernen )); then
echo "Welchen Prozess willst du entfernen:?"
read antwort
p=arrayName[$antwort]
z=1
while (( $z < $p )); do
z=((z*2))
done

#TODO: stelle muss die position von dem gewünschten Prozess("p"+$antwort) in dem arraySpeicher beinhalten!
arraySpeicher[$stelle]=z

h=1
while (( $h == 1 )); do
h=2
for i in "${arraySpeicher[@]}"; do
if (( i < "${#arraySpeicher[@]}"-1 )); then
if (( arraySpeicher[$i] == arraySpeicher[$i+1] )); then
unset arraySpeicher[$i+1]
arraySpeicher[$i]=((arrayspeicher[$i]*2))
h=1
fi
fi
done 
done

fi

if (( $eingabe == aufhören )); then
break
fi
done
done

edit: I just put in the whole code i have for now. still some work to do, still need to add comments and indent the code. hopefully this script gets done! thx for the help in advance!

Upvotes: 1

Views: 60

Answers (1)

choroba
choroba

Reputation: 241898

The problem is that you don't assign a result at the following line

j=j/2

Change it to

((j=j/2))

When you assing the string j/2 to $j, using j/2 as an arithmetic expression (in the while condition) tries to expand $j, but it expands to j/2, so it tries to expand $j, etc.

Upvotes: 1

Related Questions