Brad Diggs schrieb:

In short the bug is the result of failure to expand the
subscript of an array if the subscript is a variable.
The following script should return a list of files with a preceding (File <#>: ). However, it does not work that way because the integer variable (${d}) used in the subscript of the array statement (FileList[${d}]=${File}) does not get properly expanded.

#!/bin/bash
declare -a FileList=('')
declare -i d=0

ls -1| while read File
do
   FileList[${d}]=${File}
   d=$((10#${d}+1))
done

This is normal bash behaviour, see FAQ E4. As bash executes _all_ parts of a pipe in subshells (in contrast to ksh, where the last component is executed in the current shell), the variable 'FileList' being assigned here is local to the subshell. After the loop the variable 'FileList' declared in line 1 (which happens to have the same name, but that doesn't matter) is unchanged.

Try this instead:

        while read File
        do
           FileList[d]=$File
           (( d=d+1 ))
        done <<<"$(ls -1)"

Greetings,
Bernd

--
Bernd Eggink
[EMAIL PROTECTED]
http://sudrala.de


Reply via email to