Вы ищете диалог
. Это очень мощный инструмент и использует ncurses
для предоставления множества опций. Я предлагаю вам прочитать его manpage. В частности, вам нужна опция -меню
:
--menu text height width menu-height [ tag item ] ...
As its name suggests, a menu box is a dialog box that can be
used to present a list of choices in the form of a menu for the
user to choose. Choices are displayed in the order given. Each
menu entry consists of a tag string and an item string. The tag
gives the entry a name to distinguish it from the other entries
in the menu. The item is a short description of the option that
the entry represents. The user can move between the menu en‐
tries by pressing the cursor keys, the first letter of the tag
as a hot-key, or the number keys 1-9. There are menu-height en‐
tries displayed in the menu at one time, but the menu will be
scrolled if there are more entries than that.
On exit the tag of the chosen menu entry will be printed on dia‐
log's output. If the "--help-button" option is given, the cor‐
responding help text will be printed if the user selects the
help button.
К сожалению, реализовать ее в здравом уме, используя вывод команды, содержащей пробелы, довольно сложно из-за различных проблем с цитированием. Во всяком случае, мне это не удалось, и мне пришлось прибегнуть к использованию -меню
. Тем не менее, она работает и делает то, что вы просили:
#!/usr/bin/env bash
tmp=$(mktemp)
IFS=
eval dialog --menu \"Please choose a filesystem:\" 50 50 10 $(lsblk -f | sed -r 's/^/"/;s/$/" " "/' | tr $'\n' ' ') 2>$tmp
D=$(tr -d '│├└─' < $tmp | sed 's/^[ \t]*//' | cut -d' ' -f1)
printf "You chose:\n%s\n" "$D"
Для более переносимого подхода, измените команду grep
на
sed
просто форматирует вывод lsblk
так, чтобы вокруг каждой строки вывода были кавычки (это "тег" диалога), за которым следует цитируемый пробел (это "элемент" диалога), а tr
заменяет новые строки пробелами и символами дерева.
Результат выглядит следующим образом:
┌────────────────────────────────────────────────┐
│ Please choose a filesystem: │
│ ┌────────────────────────────────────────────┐ │
│ │ NAME FSTYPE LABEL MOUNTPOINT │ │
│ │ sda │ │
│ │ ├─sda1 │ │
│ │ ├─sda2 │ │
│ │ ├─sda3 /winblows │ │
│ │ ├─sda4 │ │
│ │ ├─sda5 │ │
│ │ ├─sda6 /home │ │
│ │ ├─sda7 / │ │
│ │ └─sda8 [SWAP] │ │
│ └────↓(+)────────────────────────────90%─────┘ │
│ │
├────────────────────────────────────────────────┤
│ < OK > │
└────────────────────────────────────────────────┘
здесь на моем системе Debian 8, / usr / bin / write - symlink to / usr / bin / bsd-запись И иметь SETGID TTY GROUP и /DEV/TTY (0..63} - групповая запись.