如何将此多选的bash脚本默认为选定的所有项目

  • 本文关键字:项目 默认 bash 脚本 bash
  • 更新时间 :
  • 英文 :


我修改了Nathan Davieau在ServerFault.com上在线找到的一个很棒的脚本,但是我已经达到了我对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

在这里,您只需要在实际处理它们之前初始化 choices[](array)。(重新)以下修订的代码(感谢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配对)可能是解释新手的挑战。只有在您节省时间时才实验。

请注意,代码的关键位是+切换的位置:

if [[ "${choices[SELECTION]}" == "+" ]]; then
    choices[SELECTION]=""
else
    choices[SELECTION]="+"
fi

ihth

最新更新