팀, 연관 배열에 일부 변수를 설정했는데 출력이 결과를 생성하지 않습니다. 팁이 있나요? >
#/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]}