You are not logged in.
Pages: 1
Hello!
Im currently trying to build a simple bash script which simulates a Buddy algorithm.
Part of it is adding processes to a RAM and splitting the exisiting Ram in n² values. eg: if our process is 200kb big and our RAM is 1024kb large the RAM needs to be splittet in 1 partition of 512 KB and 2 of 256 KB in which one is occupied by the process.
I tried realize this by using arrays and easy math to give an Output in my shell close to this : "This is your Ram: 512, 256 and P1 after adding a Process."
The script i wrote sadly doesnt really accomplishes that:
#!/bin/bash
echo "How big is the RAM?: "
read speicher
echo "How big is the Process?: "
read prozess
arraySpeicher=($speicher)
arrayName=()
for i in "${arraySpeicher[@]}"; do
if (( $prozess <= $i )); then
j=$i
unset arraySpeicher[$j]
while (( $prozess < $j/2 )); do
arraySpeicher+=($j/2)
((j=$j/2))
done
arraySpeicher+="P${#ArrayName[@]}"
arrayName+=$prozess
fi
done
echo "This is your Ram: ${arraySpeicher[@]} after adding a Process."
Note: the variables are German (which is the language im coding in but i changed a few echos to make it a bit easier to understand where im heading at)
The output is as follows : "This is your Ram: 1024P0 1024/2 512/2 after adding a Process."
Can you guys help me out?
Regards
Jonas
Offline
Here's a link for how to do math in bash.
HTH
You're just jealous because the voices only talk to me.
Offline
Is this homework? If not, fine. It just smells a bit like it.
If it is homework, that is fine too. But be honest about it and we will provide guidance, but not direct solutions.
Thanks
Edit: Oh, and welcome to Arch Linux
Last edited by ewaller (2016-06-20 18:28:20)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Hello Member,
Thanks for your help i'm still not seeing what exactly i'm doing wrong because there are different ways of doing math in bash. People on stack overflow tell me to do it like this : (( c =a+b )). my prof says use c='expr a+b' then theres these other ways which i've tried and a few of them show me errors but most of them do not and it just compiles. How do i know which way is the correct one when you just show me another 5 ways of doing it?
Regards Jonas
Offline
Hello ewaller:
Its a project which is needed to be done so we are allowed to enter the exam, so in a way it is homework. But in reality it is much more important.
edit: thanks for the welcome!
edit 2: note that this may be necessary to enter the exam, but this scripting part is not part of the actual Exam which makes it harder to pick up when there is so much else to learn!
Last edited by ThePretender73 (2016-06-20 18:33:54)
Offline
From the linked article:
(( n += 1 )) # Increment.
# (( $n += 1 )) is incorrect!
Edit: I think your line is using the '$' (end edit)
Other comments:
arraySpeicher+=($j/2)
Double parentheses?
BTW, I am not a Bash person, so take what I write with a grain of salt. Also, I prefer to use bc or dc to perform math rather than depending on Bash natives. http://stackoverflow.com/questions/2331 … ing-and-bc
Last edited by ewaller (2016-06-20 18:49:28)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
the part with the $ is what i've already tried. It didn't change much + shouldn't j receive the value of j? so saying (( j=$j/2 )) isn't exactly wrong... + a bash expert on stack overflow told me exactly to do it in that manner. I'm just generally confused by what is correct and what is not at this point.
to arraySpeicher+=($j/2) if i do double parantheses i get another error.
Offline
You do $j/2 three times three different ways...
practise in a terminal
5:01:53 am
[ ~ ]$
└──╼ j=4; k=$((j/2)); echo $k
2
HTH
You're just jealous because the voices only talk to me.
Offline
The parentheses in
arraySpeicher+=($j/2)
are syntax for compound assignment. A sequence of words inside parentheses is like an array literal. With the += operator these elements are appended to the array. Inside the parenthese you can use the $(( )) syntax to evaluate the arithmetic expression. Or simply swap the two lines:
((j=$j/2))
arraySpeicher+=($j)
Using += without compound assignment is just appending strings, and referencing an array without a subscript is like referencing the first element, so in your example:
arraySpeicher+="P${#ArrayName[@]}"
appends "P0" to the first element "1024".
About math: $(( )) is portable (works in most shells, also FreeBSD's sh for example), expr (and bc/dc) are separate programs and (( )) (and also the weird 'let') work in bash and zsh, but not in FreeBSD's sh.
Offline
when you do the for loop, there is only one element in the array, which isn't an actual key, but rather a value the way you use it. there's no reason to set j to i, and no reason to unset arraysphincter[j] because there is probably no such key in said array. and if you unset it before every time you add a value, there's no point in it even being an array.
Last edited by ymf331 (2016-06-29 01:56:28)
i can't put this zsh config on the forum: http://dotshare.it/dots/1144/
Offline
(( c = a +b ))
is just cleaner syntax for doing the same thing as
c='expr a+b'
in more recent releases of bash.
Offline
Pages: 1