调用关联数组

调用关联数组

团队,正在关联数组中设置一些变量,但其输出没有产生任何结果......有任何提示吗?>

#/bin/bash

#IOEngine="psync"
#TestType="read"
IOEngine="libaio"
TestType="randread"

vars_ioengine_defaults() {
declare -A associative_vars
  RunTime="0"
  UDCNAme="stage"
  if [[ "$IOEnginge" == "psync" ]]  && [[ "$TestType" == "read" ]]; then
    declare -A associative_vars=([DFLT_QueueDepth]="0" [DFLT_DatasetSize]="3G" [DFLT_BlockSize]="2,4,8,16,32,64,128,256,512,1024" [DFLT_Threads]="1,2,4,8,16,32,64,128,256" [DFLT_FileSize]="3M")
  elif [[ "$IOEngine" == "psync" ]]  && [[ "$TestType" == "randread" ]]; then
    declare -A associative_vars=([DFLT_QueueDepth]="0" [DFLT_DatasetSize]="1G" [DFLT_BlockSize]="8,16,32" [DFLT_Threads]="16,32,64,128,256" [DFLT_FileSize]="32k")
  elif [[ "$IOEngine" == "libaio" ]]  && [[ "$TestType" == "read" ]]; then
    declare -A associative_vars=([DFLT_QueueDepth]="16" [DFLT_DatasetSize]="3G" [DFLT_BlockSize]="2,4,8,16,32,64,128,256,512,1024" [DFLT_Threads]="1,2,4,8,16,32,64,128,256" [DFLT_FileSize]="3M")
  elif [[ "$IOEngine" == "libaio" ]]  && [[ "$TestType" == "randread" ]]; then
    declare -A associative_vars=([DFLT_QueueDepth]="16" [DFLT_DatasetSize]="1G" [DFLT_BlockSize]="8,16,32" [DFLT_Threads]="16,32,64,128,256" [DFLT_FileSize]="32k")
  else
    echo " Neither IOEngine nor TestType variables matched to required  values"
  fi
}

vars_ioengine_defaults
echo fio_gen ${associative_vars[DFLT_QueueDepth]} ${associative_vars[DFLT_DatasetSize]}

输出:

prints nothing: no output here <<

预期输出:

fio_gen 16 1G

答案1

您的变量仅在您的函数中可见。如果您在主作用域中定义变量并在函数中分配值,则它会起作用:

#/bin/bash

#IOEngine="psync"
#TestType="read"
IOEngine="libaio"
TestType="randread"

declare -A associative_vars

vars_ioengine_defaults() {
  RunTime="0"
  UDCNAme="stage"
  if [[ "$IOEnginge" == "psync" ]]  && [[ "$TestType" == "read" ]]; then
    associative_vars=([DFLT_QueueDepth]="0" [DFLT_DatasetSize]="3G" [DFLT_BlockSize]="2,4,8,16,32,64,128,256,512,1024" [DFLT_Threads]="1,2,4,8,16,32,64,128,256" [DFLT_FileSize]="3M")
  elif [[ "$IOEngine" == "psync" ]]  && [[ "$TestType" == "randread" ]]; then
    associative_vars=([DFLT_QueueDepth]="0" [DFLT_DatasetSize]="1G" [DFLT_BlockSize]="8,16,32" [DFLT_Threads]="16,32,64,128,256" [DFLT_FileSize]="32k")
  elif [[ "$IOEngine" == "libaio" ]]  && [[ "$TestType" == "read" ]]; then
    associative_vars=([DFLT_QueueDepth]="16" [DFLT_DatasetSize]="3G" [DFLT_BlockSize]="2,4,8,16,32,64,128,256,512,1024" [DFLT_Threads]="1,2,4,8,16,32,64,128,256" [DFLT_FileSize]="3M")
  elif [[ "$IOEngine" == "libaio" ]]  && [[ "$TestType" == "randread" ]]; then
    associative_vars=([DFLT_QueueDepth]="16" [DFLT_DatasetSize]="1G" [DFLT_BlockSize]="8,16,32" [DFLT_Threads]="16,32,64,128,256" [DFLT_FileSize]="32k")
  else
    echo " Neither IOEngine nor TestType variables matched to required  values"
  fi
}

vars_ioengine_defaults
echo fio_gen ${associative_vars[DFLT_QueueDepth]} ${associative_vars[DFLT_DatasetSize]}

相关内容