目标
我用来让用户将密码插入与某些模板文件whiptail
一起使用的文件中。envsubst
whiptail
清单
为了渲染东西,whiptail
我使用以下内容:
declare -A availableServices=(
[grafana]="Grafana Dashboard"
[influxdb]="InfluxDB v1.x TSDB"
[node-red]="Node-RED Flow UI"
[portainer]="Portainer Container Mgmt"
)
# Render TUI
function askGenerateCredential() {
local message="Set Password for particular Service\n
Press <SPACEBAR> to Select \n
Press <Enter> to Skip
"
local arglist=()
# Generate a String for Whiptail Checkboxes
# FORMAT: "<INDEX> <DESCRIPTION> <OFF>"
for index in "${!availableServices[@]}";
do
# Default all Services are NOT-Selected (OFF)
arglist+=("$index" "${availableServices[$index]}" "OFF")
done
SELECTED_SERVICES+=$($WHIPTAIL --title "Available Services within Populo" \
--notags --separate-output \
--ok-button Next \
--nocancel \
--checklist "$message" $LINES $COLUMNS $(( LINES - 12 )) \
-- "${arglist[@]}" \
3>&1 1>&2 2>&3)
}
theSELECTED_SERVICES
是一个声明为的数组
declare -a SELECTED_SERVICES=()
渲染效果很好,但是我的if else
脚本中有一个特定条件,无法根据上面数组中的选定服务执行特定命令。
askGenerateCredential
if [ -z "$SELECTED_SERVICES" ]; then
echo "No Services were selected. Exiting..."
exit 1
else
for service in "${SELECTED_SERVICES}";
do
if [ "$service" == "influxdb" ]; then
echo "Setting Credentials for InfluxDB"
setInfluxDBCredentials
elif [ "$service" == "grafana" ]; then
echo "Setting Credentials for Grafana"
setGrafanaCredentials
elif [ "$service" == "node-red" ]; then
echo "Setting Credentials for node-RED"
setNodeRedCredentials
elif [ "$service" == "portainer" ]; then
echo "Setting Credential for Portainer"
setPortainerCredentials
fi
done
fi
添加上述块后,当我通过UI0
选择多个值时,我的脚本会以代码退出。whiptail
相反,当我在 UI 中只选择一个值时,会调用相应的函数。
我在这里做错了什么?我希望根据service
变量,调用相应的函数而不退出脚本。
代码
#!/bin/env bash
WHIPTAIL=$(which whiptail)
if [ -z $WHIPTAIL ]
then
echo "This script requires whiptail to render the TUI."
exit 1
fi
declare -a SELECTED_SERVICES=()
LINES=$(tput lines)
COLUMNS=$(tput cols)
declare -A availableServices=(
[grafana]="Grafana Dashboard"
[influxdb]="InfluxDB v1.x TSDB"
[node-red]="Node-RED Flow UI"
[portainer]="Portainer Container Mgmt"
)
function askGenerateCredential() {
local message="Set Password for particular Service\n
Press <SPACEBAR> to Select \n
Press <Enter> to Skip
"
local arglist=()
# Generate a String for Whiptail Checkboxes
# FORMAT: "<INDEX> <DESCRIPTION> <OFF>"
for index in "${!availableServices[@]}";
do
# Default all Services are NOT-Selected (OFF)
arglist+=("$index" "${availableServices[$index]}" "OFF")
done
SELECTED_SERVICES+=$($WHIPTAIL --title "Available Services within Populo" \
--notags --separate-output \
--ok-button Next \
--nocancel \
--checklist "$message" $LINES $COLUMNS $(( LINES - 12 )) \
-- "${arglist[@]}" \
3>&1 1>&2 2>&3)
}
function setInfluxDBCredential() {
echo "set admin password env var for influxdb"
}
function setGrafanaCredential() {
echo "set admin password env var for grafana"
}
function setNodeRedCredential() {
echo "set admin password env var for node-REd"
}
function setPortainerCredential() {
echo "set password file for portainer"
}
askGenerateCredential
if [ -z "$SELECTED_SERVICES" ]; then
echo "No Services were selected. Exiting..."
exit 1
else
for service in "${SELECTED_SERVICES}";
do
if [ "$service" == "influxdb" ]; then
echo "Setting Credentials for InfluxDB"
setInfluxDBCredentials
elif [ "$service" == "grafana" ]; then
echo "Setting Credentials for Grafana"
setGrafanaCredentials
elif [ "$service" == "node-red" ]; then
echo "Setting Credentials for node-RED"
setNodeRedCredentials
elif [ "$service" == "portainer" ]; then
echo "Setting Credential for Portainer"
setPortainerCredentials
fi
done
fi
编辑
根据建议,我${SELECTED_SERVICES[@]}
在 for 循环中进行了尝试,并添加了一个echo "$service"
能够循环遍历数组的函数,但是if
条件不会被触发。
我相信你的
askGenerateCredential()
函数中的逻辑没有做你想做的事。目前,whiptail 命令的整个输出都作为单个元素添加到数组中。如果您想在换行符处拆分多个元素,则应将其更改为以下内容:请参阅4.2 Bash 内置命令
您也将
SELECTED_SERVICES
其作为变量而不是数组调用。你需要改变:
至:
照原样,您只会扩展数组的第一个元素。但是,当您编写代码时,
SELECTED_SERVICES
只会收到一个元素。见6.7 数组
此外,可能会更好地使用 case 来代替 if/elif 构造。就像是: