Arrays in dash, (POSIX).
Hi gurus...
I am thinking of trying AudioScope.sh in pure POSIX so...
I need an array in dash, I know it is not possible but pseudo-arrays are.
I have two versions that work, the second is an idea from the WWW.
The first is what I would like to use.
There are problems in both...
The first has two problems:
1) It needs "eval" to create a number of variables "VAR0, VAR1, VAR2 ... VARn".
AND
2) I have no idea if there is a limit to how many variables POSIX allows.
The minimum I would need is 48000 and the maximum 65536.
The second is a modified version from the WWW and has a major problem as well as 'eval' and the 'maximum limit' too.
But the main one is I have no idea how to make it mutable.
So the questions are:
Is there a better method than either of mine that creates a pseudo-array that is fully mutable?
Is there a limit to the number of variables to create a pseudo-array in POSIX?
Is it possible to eliminate 'eval' to create a pseudo-array, (I suspect the answer is no)...
TIA...
This is mutable...
#!/usr/local/bin/dash
# MUTABLE! ;o)
# 'dash' Version 0.5.9
/bin/echo 'ab
b c
c$d
de
e \f\n
f/g
g~ h
h*i
i#j
j%k
kl
lm' > /tmp/array.txt
# *******************************************
# The main array generator,,,
INDEX=0
while IFS=$'\n' read -r ARRAY
do
eval "MY_ARRAY${INDEX}='${ARRAY}'"
INDEX=$(( INDEX+1 ))
done < /tmp/array.txt
# *******************************************
LENGTH=${INDEX}
/bin/echo "Array_length = ${LENGTH}"
# Basic test.
/bin/echo "Original array index 4 = ${MY_ARRAY4}"
MY_ARRAY4='1 2 3'
/bin/echo "Modified array index 4 = ${MY_ARRAY4}"
/bin/echo ""
INDEX=0
while [ ${INDEX} -le 12 ]
do
eval /bin/echo "\${MY_ARRAY${INDEX}}"
INDEX=$(( INDEX+1 ))
done
This is immutable.
#!/usr/local/bin/dash
# IMMUTABLE! ;o(
# 'dash' Version 0.5.9
/bin/echo 'a b,bc,cd \f\n,de,ef,f g,gh,h\i,ij,jk,kl,lm' > /tmp/array.txt
ARRAY=$( cat /tmp/array.txt )
orig_ifs="$IFS"
IFS=','
set -- $ARRAY
INDEX=1
while [ ${INDEX} -le 12 ]
do
eval VAL=$( /bin/echo "\${${INDEX}}" )
printf "%u = %s\n" "${INDEX}" "${VAL}"
INDEX=$(( INDEX+1 ))
done
LENGTH=$(( INDEX-1 ))
#*Just basic tests.
/bin/echo ""
/bin/echo "Array_length = ${LENGTH}"
INDEX=10
eval /bin/echo "\${${INDEX}}"
INDEX=3
eval /bin/echo "\${${INDEX}}"
/bin/echo ""
IFS="$orig_ifs"