简体   繁体   English

数组的值未递增

[英]Value of Array not being incremented

I am attempting to increment the index of my array using the following way however it seems to overwrite the previous index which is wrong. 我正在尝试使用以下方式增加数组的索引,但是它似乎会覆盖先前的索引,这是错误的。 Any suggestions on what I might be doing wrong. 关于我可能做错的任何建议。

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}"

The above code is actually being used here and I could tell the array index is being overwritten. 上面的代码实际上是在这里使用的,我可以知道数组索引正在被覆盖。 Every-time a new entry is made in an array it is suppose to extract the current index then increment it by one and then use that index.However it looks like it is not doing that. 每次在数组中创建一个新条目时,都假定要提取当前索引,然后将其递增一个,然后再使用该索引。但是看起来它没有这样做。 Any suggestions on why the arrays are overwriting their previous index ? 关于数组为何覆盖其先前索引的任何建议?

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}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM