Bash选择没有标签的菜单格式

时间:2016-06-03 14:06:54

标签: bash

我在Bash中构建了一个简单的选择菜单。它当前正在水平显示菜单(带标签),如下所示:

1) Create a VM from scratch   3) Command-line Usage
2) Management Menu            4) Quit

我希望列表看起来像这样:

1) Create a VM from scratch  
2) Management Menu
3) Command-line Usage     
4) Quit

更新:这是我的代码:

PS3="Please choose a valid option : "
OPTIONS=("Create a VM from scratch" "Management Menu" "Command-line Usage" "Quit")
select opt in "${OPTIONS[@]}"; do
    case $opt in
            "Create a VM from scratch")
                 createit
                 exit
                 ;;
            "Management Menu")
                 mgmtmenu
                 exit
                 ;;
            "Command-line Usage ")
                 help
                 ;;
            "Quit")
                exit
                ;;
            *) echo invalid option;;
    esac
done

如何在每个选项上显示选择菜单?

1 个答案:

答案 0 :(得分:6)

Bash定义了由$COLUMNS读取的select环境变量。

正如bash' man中所见:

COLUMNS
         Used by the select compound command to  determine  the  terminal
         width  when  printing selection lists.  Automatically set if the
         checkwinsize option is enabled or in an interactive  shell  upon
         receipt of a SIGWINCH.