jim*_*mij 48
dialog --backtitle "Package configuration" \
--title "Configuration sun-java-jre" \
--yesno "\nBla bla bla...\n\nDo you accept?" 10 30
Run Code Online (Sandbox Code Playgroud)
用户响应存储在退出代码中,因此可以像往常一样打印:(echo $?注意这0意味着“是”,而1在 shell 世界中是“否”)。
关于评论部分的其他问题:
要将某些命令的输出放入对话框中,只需使用命令替换机制$(),例如:
dialog --backtitle "$(echo abc)" --title "$(cat file)" ...
Run Code Online (Sandbox Code Playgroud)为用户提供多种选择,您可以使用--menuoption 而不是--yesno
要将用户选择的输出存储到变量中,需要使用--stdout选项或通过--output-fd或手动更改输出描述符,例如:
output=$(dialog --backtitle "Package configuration" \
--title "Configuration sun-java-jre" \
--menu "$(parted -l)" 15 40 4 1 "sda1" 2 "sda2" 3 "sda3" \
3>&1 1>&2 2>&3 3>&-)
echo "$output"
Run Code Online (Sandbox Code Playgroud)
这个技巧是必需的,因为dialog默认情况下输出到 stderr,而不是 stdout。
和往常一样,man dialog是你的朋友。
Tho*_*key 37
问题中的屏幕截图看起来像whiptail(一个功能减少的程序模仿dialog,使用 newt 而不是ncurses)。标题和按钮的呈现方式内置于每个程序中,使它们看起来不同。
这是一个复制原始屏幕截图的脚本,用于鞭尾或对话框:
#!/bin/sh
: ${DIALOG:=dialog}
case "$DIALOG" in
*dialog*)
OPTS="$OPTS --cr-wrap"
high=10
;;
*whiptail*)
high=12
;;
esac
rows=$(stty size | cut -d' ' -f1)
[ -z "$rows" ] && rows=$high
[ $rows -gt $high ] && rows=$high
cols=$(stty size | cut -d' ' -f2)
$DIALOG --backtitle "Package configuration" \
--title "Configuring sun-java6-jre" \
$OPTS \
--yesno '\nIn order to install this package, you must accept the license terms, the "Operating System Distributor License for Java" (DLJ), v1.1. Not accepting will cancel the installation.\n\nDo you accept the DLJ license terms?' $rows $((cols - 5))
Run Code Online (Sandbox Code Playgroud)
为了进行比较,使用whiptail截图:
并带有对话框:
除了标题和按钮的不同外观外,对话框默认使用不同的颜色(尽管这是可配置的 -请参阅屏幕截图),并且在屏幕上使用较少的行。
dialog(和whiptail)使用库来管理线条、颜色等的显示。但是您可能还会看到在Red Hat anaconda 程序中使用newt 作为从python调用的共享库(具有相同的外观)。沿着同样的思路,内核配置程序开始是一个(缩减的)对话框副本,然后演变成使用共享库(没有原始lxdialog程序)的功能,就像在 python 中使用 newt 的方式一样。
在 bash 中——对于最常用的功能,您可以使用对话框或鞭尾。有人为那些(在 perl 中)编写了一个包装器,以允许脚本更容易地使用这些或其他一些,但最好直接使用对话框,因为 perl 模块本质上是公分母。
对话框源包括所有小部件的示例以及大多数命令行选项:
cdialog (ComeOn Dialog!) version 1.3-20160424
Copyright 2000-2015,2016 Thomas E. Dickey
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Display dialog boxes from shell scripts *
Usage: cdialog <options> { --and-widget <options> }
where options are "common" options, followed by "box" options
Special options:
[--create-rc "file"]
Common options:
[--ascii-lines] [--aspect <ratio>] [--backtitle <backtitle>] [--beep]
[--beep-after] [--begin <y> <x>] [--cancel-label <str>] [--clear]
[--colors] [--column-separator <str>] [--cr-wrap] [--date-format <str>]
[--default-button <str>] [--default-item <str>] [--defaultno]
[--exit-label <str>] [--extra-button] [--extra-label <str>]
[--help-button] [--help-label <str>] [--help-status] [--help-tags]
[--hfile <str>] [--hline <str>] [--ignore] [--input-fd <fd>]
[--insecure] [--item-help] [--keep-tite] [--keep-window] [--last-key]
[--max-input <n>] [--no-cancel] [--no-collapse] [--no-cr-wrap]
[--no-items] [--no-kill] [--no-label <str>] [--no-lines] [--no-mouse]
[--no-nl-expand] [--no-ok] [--no-shadow] [--no-tags] [--nook]
[--ok-label <str>] [--output-fd <fd>] [--output-separator <str>]
[--print-maxsize] [--print-size] [--print-version] [--quoted]
[--scrollbar] [--separate-output] [--separate-widget <str>] [--shadow]
[--single-quoted] [--size-err] [--sleep <secs>] [--stderr] [--stdout]
[--tab-correct] [--tab-len <n>] [--time-format <str>] [--timeout <secs>]
[--title <title>] [--trace <file>] [--trim] [--version] [--visit-items]
[--week-start <str>] [--yes-label <str>]
Box options:
--buildlist <text> <height> <width> <list-height> <tag1> <item1> <status1>...
--calendar <text> <height> <width> <day> <month> <year>
--checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...
--dselect <directory> <height> <width>
--editbox <file> <height> <width>
--form <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>...
--fselect <filepath> <height> <width>
--gauge <text> <height> <width> [<percent>]
--infobox <text> <height> <width>
--inputbox <text> <height> <width> [<init>]
--inputmenu <text> <height> <width> <menu height> <tag1> <item1>...
--menu <text> <height> <width> <menu height> <tag1> <item1>...
--mixedform <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>...
--mixedgauge <text> <height> <width> <percent> <tag1> <item1>...
--msgbox <text> <height> <width>
--passwordbox <text> <height> <width> [<init>]
--passwordform <text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>...
--pause <text> <height> <width> <seconds>
--prgbox <text> <command> <height> <width>
--programbox <text> <height> <width>
--progressbox <text> <height> <width>
--radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...
--rangebox <text> <height> <width> <min-value> <max-value> <default-value>
--tailbox <file> <height> <width>
--tailboxbg <file> <height> <width>
--textbox <file> <height> <width>
--timebox <text> <height> <width> <hour> <minute> <second>
--treeview <text> <height> <width> <list-height> <tag1> <item1> <status1> <depth1>...
--yesno <text> <height> <width>
Auto-size with height and width = 0. Maximize with height and width = -1.
Global-auto-size if also menu_height/list_height = 0.
Run Code Online (Sandbox Code Playgroud)
进一步阅读:
Tha*_*awn 14
我相信您正在寻找的包是ncurses。
维基百科对 ncurses 的描述如下:
ncurses(新的curses)是一个提供API 的编程库,允许程序员以独立于终端的方式编写基于文本的用户界面。它是一个用于开发在终端模拟器下运行的“类GUI”应用软件的工具包
由于您使用的是 bash,您可以使用Bash Simple Curses(如 Runium 在下面的评论中提到的)。