0

Team, am setting some variables in an associative array but its output is not resulting anything.. any hint?>

#/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]}

output:

prints nothing: no output here <<

expected output:

fio_gen 16 1G
2
  • Is that the actual script you're running? How are you running it? Commented Jun 13, 2019 at 3:14
  • its already resolved below. not sure why closed. Commented Jun 25, 2019 at 16:32

1 Answer 1

1

Your variable is only visible in your function. It works if you define the variable in the main scope and assign the values in the function:

#/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]}
3
  • trying to figure out if there is any issue in your syntax in the IFs.. observe some $ with spaces. or are you still formatting your script? ex: [DFLT_BlockSize]="2,4,8,16,32,64,128,256,512,1024" $ not sure if this $ is needed here Commented Jun 11, 2019 at 2:05
  • oh u meant to use ) instead of $? Commented Jun 11, 2019 at 2:07
  • Edited! No, just move declare -A associative_vars out of the function and remove the declare -A when you assign the values. Commented Jun 11, 2019 at 2:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.