我在serverfault.com上修改了Nathan Davieau在网上找到的一个很棒的剧本,但是我对Bash的知识达到了极限!
它很棒,除了我必须选择每个项目,我希望从选择的所有项目开始,并且必须取消选择它们。
#!/bin/bash
ERROR=" "
declare -a install
declare -a options=('php' 'phpmyadmin' 'php-mysqlnd' 'php-opcache' 'mariadb-server' 'sendmail')
#=== FUNCTION ========================================================================
# NAME: ACTIONS
# DESCRIPTION: Actions to take based on selection
# PARAMETER 1:
#=======================================================================================
function ACTIONS() {
for ((i = 0; i < ${#options[*]}; i++)); do
if [[ "${choices[i]}" == "+" ]]; then
install+=(${options[i]})
fi
done
echo "${install[@]}"
}
#=== FUNCTION ========================================================================
# NAME: MENU
# DESCRIPTION: Ask for user input to toggle the name of the plugins to be installed
# PARAMETER 1:
#=======================================================================================
function MENU() {
echo "Which packages would you like to be installed?"
echo
for NUM in "${!options[@]}"; do
echo "[""${choices[NUM]:- }""]" $((NUM + 1))") ${options[NUM]}"
done
echo "$ERROR"
}
#Clear screen for menu
clear
#Menu loop
while MENU && read -e -p "Select the desired options using their number (again to uncheck, ENTER when done): " -n2 SELECTION && [[ -n "$SELECTION" ]]; do
clear
if [[ "$SELECTION" == *[[:digit:]]* && $SELECTION -ge 1 && $SELECTION -le ${#options[@]} ]]; then
((SELECTION--))
if [[ "${choices[SELECTION]}" == "+" ]]; then
choices[SELECTION]=""
else
choices[SELECTION]="+"
fi
ERROR=" "
else
ERROR="Invalid option: $SELECTION"
fi
done
ACTIONS
答案 0 :(得分:1)
在这里,您只需在实际处理它们之前初始化choices[]
(数组)。 (重新)修改后的代码(感谢Charles Duffy):
#!/bin/bash
ERROR=" "
declare -a install
declare -a options=('php' 'phpmyadmin' 'php-mysqlnd' 'php-opcache' 'mariadb-server' 'sendmail')
############### HERE's THE NEW BIT #################
declare -a choices=( "${options[@]//*/+}" )
####################################################
#=== FUNCTION ========================================================================
# NAME: ACTIONS
# DESCRIPTION: Actions to take based on selection
# PARAMETER 1:
#=======================================================================================
function ACTIONS() {
for ((i = 0; i < ${#options[*]}; i++)); do
if [[ "${choices[i]}" == "+" ]]; then
install+=(${options[i]})
fi
done
echo "${install[@]}"
}
#=== FUNCTION ========================================================================
# NAME: MENU
# DESCRIPTION: Ask for user input to toggle the name of the plugins to be installed
# PARAMETER 1:
#=======================================================================================
function MENU() {
echo "Which packages would you like to be installed?"
echo
for NUM in "${!options[@]}"; do
echo "[""${choices[NUM]:- }""]" $((NUM + 1))") ${options[NUM]}"
done
echo "$ERROR"
}
#Clear screen for menu
clear
#Menu loop
while MENU && read -e -p "Select the desired options using their number (again to uncheck, ENTER when done): " -n2 SELECTION && [[ -n "$SELECTION" ]]; do
clear
if [[ "$SELECTION" == *[[:digit:]]* && $SELECTION -ge 1 && $SELECTION -le ${#options[@]} ]]; then
((SELECTION--))
if [[ "${choices[SELECTION]}" == "+" ]]; then
choices[SELECTION]=""
else
choices[SELECTION]="+"
fi
ERROR=" "
else
ERROR="Invalid option: $SELECTION"
fi
done
ACTIONS
很抱歉,但是我没有时间对所有这些如何运作的描述进行打击。在这种情况下,set -vx
(与set +vx
配对)的传统shell调试工具可能是解释新手的挑战。只有在你可以节省时间时才进行实验。
请注意,代码的关键位是+
切换的位置:
if [[ "${choices[SELECTION]}" == "+" ]]; then
choices[SELECTION]=""
else
choices[SELECTION]="+"
fi
IHTH