首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使这个多选择Bash脚本默认为所有选定的项目?

如何使这个多选择Bash脚本默认为所有选定的项目?
EN

Stack Overflow用户
提问于 2017-11-30 10:29:07
回答 1查看 387关注 0票数 1

我修改了Nathan在serverfault.com上在网上找到的一个很棒的脚本,但是我已经达到了我对Bash的认识的极限!

它工作得很好,除了我必须选择每一项之外,我希望它以开始,从所有选定的开始,并且必须取消选择它们。

代码语言:javascript
复制
#!/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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-30 17:38:02

在这里,您只需要在实际处理choices[] (数组)之前初始化它们。(重新)修改后的代码(感谢查尔斯·达菲):

代码语言:javascript
复制
#!/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的传统shell调试工具(与set +vx配对)对于新手来说可能是一个挑战。只有在你能抽出时间的时候才做实验。

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

代码语言:javascript
复制
if [[ "${choices[SELECTION]}" == "+" ]]; then
    choices[SELECTION]=""
else
    choices[SELECTION]="+"
fi

IHTH

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47571175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档