Hi, just a few minor suggestions:
1/ I would suggest that you use proper indentation to make the code more readable.
2/ When debugging shell scripts, it is a good idea to put
set -xv at the begining of the script. That will allow you to follow each step the script does.
3/ When describing your problem, be more specific than "
It doesn't work" or "
I'm not getting correct results". What exactly does not work? What results are you getting and what results do you expect?
4/
Code:
arrayName=(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
You can use
brace expansion here:
Code:
arrayName=({1..20})
5/
Code:
var=$(echo "${#arrayName[@]}")
That's a very useless use of
echo. just do
Code:
var="${#arrayName[@]}"
6/
That's an infinite loop. Please make sure that the loop exits when its purpose is achieved.
7/
Code:
unset arrayName[$i]
I would recommend putting
"arrayName[$i]" within double quotes to avoid pathname expansion. Also, keep in mind
unsetting an array member will not reset numbering of the other elements.
8/
better written as
or, even better
9/
Don't forget array indices start at 0 in bash.
10/ I'll leave fixing the remaining flaws in the logic of the program as an exercise for the
original poster.