Arrays 未递增的数组值
我正在尝试使用以下方法增加数组的索引 然而,它似乎覆盖了以前的索引,这是错误的。任何关于我可能做错什么的建议Arrays 未递增的数组值,arrays,bash,Arrays,Bash,我正在尝试使用以下方法增加数组的索引 然而,它似乎覆盖了以前的索引,这是错误的。任何关于我可能做错什么的建议 CURRENT_INDEX=${#ARRAY_EXIST_ONLY_IN_STEPPER[@]} ((CURRENT_INDEX++)) ARRAY_SAMENAME_SAME_SIZE[${CURRENT_INDEX}]=${FILENAMEONLY} echo "File is present in both and both file sizes are the same : "
CURRENT_INDEX=${#ARRAY_EXIST_ONLY_IN_STEPPER[@]}
((CURRENT_INDEX++))
ARRAY_SAMENAME_SAME_SIZE[${CURRENT_INDEX}]=${FILENAMEONLY}
echo "File is present in both and both file sizes are the same : "${FILENAMEONLY} " Placed in ${CURRENT_INDEX}"
上面的代码实际上正在这里使用,我可以看出数组索引正在被覆盖。每次在数组中创建一个新条目时,假定它提取当前索引,然后将其递增1,然后使用该索引。但是看起来它没有这样做。关于为什么数组会覆盖它们以前的索引,有什么建议吗
declare -a ARRAY_SAMENAME_DIFFERENT_SIZE;
declare -a ARRAY_SAMENAME_SAME_SIZE;
declare -a ARRAY_EXIST_ONLY_IN_STEPPER
for var in ${ARRAY_WAV_FILES_IN_STEPPER[@]}
do
FILENAMEEXTENSION=${var##*/}
FILENAMEONLY=${FILENAMEEXTENSION%.*}
################################################################################
FIND_COMMAND_RESULT=""
FindCommand="find ${ARRAY[1]} -iname ${FILENAMEEXTENSION}"
FIND_COMMAND_RESULT=$(${FindCommand})
echo "Working with : "${var}
if [[ ! -z ${FIND_COMMAND_RESULT} ]]
then
#The file is present in the 0th Array index directory
#check if they are the same size
CURRENT_FILESIZE=$(stat -c%s ${var})
REMOTE_FILESIZE=$(stat -c%s ${FIND_COMMAND_RESULT})
if [ ${CURRENT_FILESIZE} -eq ${REMOTE_FILESIZE} ]
then
#The file is present in both and both file sizes are the same - Record it
CURRENT_INDEX=${#ARRAY_EXIST_ONLY_IN_STEPPER[@]}
((CURRENT_INDEX++))
ARRAY_SAMENAME_SAME_SIZE[${CURRENT_INDEX}]=${FILENAMEONLY}
echo "File is present in both and both file sizes are the same : "${FILENAMEONLY} " Placed in ${CURRENT_INDEX}"
else
#The file is present in both but they have different sizes
echo "The file is present in both but they have different sizes : " ${var}
CURRENT_INDEX=${#ARRAY_SAMENAME_DIFFERENT_SIZE[@]}
((CURRENT_INDEX++))
ARRAY_SAMENAME_DIFFERENT_SIZE[${CURRENT_INDEX}]=${FILENAMEONLY}
fi
else
#The file exists only in stepper
echo "The file is only in stepper : " ${var}
CURRENT_INDEX=${#ARRAY_EXIST_ONLY_IN_STEPPER[@]} ; ((CURRENT_INDEX++))
ARRAY_EXIST_ONLY_IN_STEPPER[${CURRENT_INDEX}]=${FILENAMEONLY}
fi
done
有一种更简单的方法可以将项添加到数组中:
ARRAY_SAMENAME_SAME_SIZE+=("${FILENAMEONLY}")
@Gene,一个原因是shell在每个系统上都可用,但Perl并非如此。另一个原因是人们试图像编写C一样编写shell脚本。