团队,我有一组在此脚本 A 中定义的参数,它们的默认值为“DEFAULT”。当用户将这些变量参数设置为“DEFAULT”以外的任何值时,我必须运行另一个具有更新变量值的脚本,否则我将运行另一个具有默认值的脚本-B。所以,我有三种情况
我在脚本中模拟詹金斯参数设置。默认值是在设置为环境变量的 jenkins 作业上预设的,然后运行此脚本。因此,如果用户不更改任何参数并将所有 fio_gen 的值设置为默认值,那么它们将被我在脚本中定义的值替换。
1-Run Script-B with DEFAULT variable values
2-Run Script-B with all-non-DEFAULT variable values
3-Run Script-B with mixed variable values.. some default and some non.
我的代码在下面,它确定哪些变量是默认变量而不是默认变量,但我想不出为上面的混合做 case-3 的逻辑。
我应该在 vars_ioengine_defaults 中使用关联数组并进行比较和使用吗?或者什么是好的方法?
#!/bin/bash
declare -a vars=(IOEngine TestType Threads BlockSize FileSize DatasetSize QueueDepth RunTime UDCName)
for var_name in "${vars[@]}"
do
if [ -z "${!var_name}" ]; then
echo "Missing environment variable $var_name"
exit 1
fi
done
#***Comment unComment to test 3 cases"
#DatasetSize="DEFAULT"
#BlockSize="DEFAULT"
#DatasetSize="nonDEFAULT"
#BlockSize="nonDEFAULT"
DatasetSize="DEFAULT"
BlockSize="NON-DEFAULT"
preset="DEFAULT"
declare -a dynamic_vars=( DatasetSize BlockSize )
number_of_dynamic_vars="${#dynamic_vars[@]}"
number_of_default_values=0
overwrite_all() {
printf "all non-defaults variables\n"
fio_gen
}
overwrite_some() {
printf "some defaults variables\n"
for [[ "${!var}" == "$preset" ]]
do
# SOME LOGIC HERE TO replace non-default and call this function
fio_gen
done
}
overwrite_none() {
printf " all defaults variables\n"
vars_ioengine_defaults
fio_gen
}
check_vars() {
for var in "${dynamic_vars[@]}"
do
[[ "${!var}" == "$preset" ]] && ((number_of_default_values++))
done
if [[ "$number_of_default_values" -eq 0 ]]; then
echo "None var is default"
overwrite_all
elif [[ "$number_of_default_values" -eq "$number_of_dynamic_vars" ]]; then
echo "All defaults"
overwrite_none
else
echo "Mixed"
overwrite_some
echo $var
fi
}
vars_ioengine_defaults() {
RunTime="0"
UDCNAme="stage"
if [[ "$IOEnginge" == "psync" ]] && [[ "$TestType" == "read" ]]; then
QueueDepth="0"
DatasetSize="3G"
BlockSize="2,4,8,16,32,64,128,256,512,1024"
Threads="1,2,4,8,16,32,64,128,256"
FileSize="3M"
elif [[ "$IOEngine" == "psync" ]] && [[ "$TestType" == "randread" ]]; then
QueueDepth="0"
DatasetSize="1G"
BlockSize="8,16,32"
Threads="16,32,64,128,256"
FileSize="32k"
elif [[ "$IOEngine" == "libaio" ]] && [[ "$TestType" == "read" ]]; then
QueueDepth="16"
DatasetSize="3G"
BlockSize="2,4,8,16,32,64,128,256,512,1024"
Threads="1,2,4,8,16,32,64,128,256"
FileSize="3M"
elif [[ "$IOEngine" == "libaio" ]] && [[ "$TestType" == "randread" ]]; then
QueueDepth="16"
DatasetSize="1G"
BlockSize="8,16,32"
Threads="16,32,64,128,256"
FileSize="32k"
fi
}
fio_gen() {
echo "iteratre on -p $IOEngine -t $TestType -j $Threads -b $BlockSize -s $FileSize -d $DatasetSize -q $QueueDepth -r $RunTime -u $UDCName"
}
check_vars