I’ve got a little problem with the variable name concatenation oder replacement in BASH. Given is:
#!/bin/bash
Audio1=(0:ita, 96k, AAC, 2.0, s16le, 48000) # audio stream definitions
Audio2=(1:rus, 128k, AAC, 2.0, s16le, 48000) # for use in ffmpeg or
Audio3=(2:klg, 96k, AAC, 1.0, s16le, 48000) # avconv
# and so on, now processing the audio streams
for ((i=1 ; i<=8 ; i++)) ; do
Audio="Audio$i"
Audio=${!Audio}
if [ ${#Audio[0]} -gt 0 ] ; then
# do the job with $Audio, here examplaryly:
echo ${Audio[@]}
fi
done
Expected as result was:
0:ita, 96k, AAC, 2.0, s16le, 48000
1:rus, 128k, AAC, 2.0, s16le, 48000
2:klg, 96k, AAC, 1.0, s16le, 48000
I got:
0:ita,
1:rus,
2:klg,
It’s only half the solution, because there is only the first element copied of each source array instead of been totally copied into the working array $Audio. In PHP I would write the variable name replacement simply by:
for ($i=1; $i<=8; $i++) {
$Audio = ${'Audio'.$i}; // ← that’s all the magic in PHP ;-)
if (isset($Audio[0])) {
// blabla
}
}
Unfortunatelly, I can’t use PHP for this application. Of course, this is one of my first scripts in BASH. Switching over to any other coding (e.g. many simple variables instead of few arrays) aren’t an option. I have to use these presets. So, where’s my fault, what’s going wrong?
Greetings - Bert