Bash脚本编写:如何使用dialog在radiolist中获取项目名称

9

我需要使用对话框界面在bash脚本中制作一个radiolist,例如,如果我有以下列表:

dialog --backtitle "OS information" \
--radiolist "Select OS:" 10 40 3 \
 1 "Linux 7.2" off \
 2 "Solaris 9" on \
 3 "HPUX 11i" off

当用户选择一个选项并按下“确定”时,我希望我的脚本能够读取该项的名称而不是项号。

这是可能的吗?

谢谢!

3个回答

4
你可以将期望的结果放在一个数组中:
array=(Linux Solaris HPUX)
var=$(dialog --backtitle "OS infomration" \
--radiolist "Select OS:" 10 40 3 \
 1 "Linux 7.2" off \
 2 "Solaris 9" on \
 3 "HPUX 11i" off >/dev/tty 2>&1 )

printf '\n\nYou chose: %s\n' "${array[var - 1]}"

我也有类似的想法,但是没有直接的解决方案吗?比如说当你使用输入框时,插入的值存储在stderr上? - Ivan
可能有,但我不知道。这可能是您最接近的解决方案。其他人可能会给出答案。 - jordanm
1
这就是我一直在寻找的答案 - 但有一个注意点,重定向语法是反过来的(常见的脚本错误),应该是 >/dev/tty 2>&1 吧?http://www.gnu.org/software/bash/manual/bashref.html#Redirections - synthesizerpatel
@synthesizerpatel - 你是对的,stderr不会被发送到tty。帖子已经编辑过了。 - jordanm

0
Termux 应用程序中,>/dev/tty 2>&1 不起作用,但 3>&1 1>&2 2>&3 3>&- 完美地工作。
这意味着:3>&1打开一个新的文件描述符,指向标准输出,1>&2将标准输出重定向到标准错误输出,2>&3将标准错误输出指向标准输出,3>&-在命令执行后删除文件描述符3。取自:BASH: Dialog input in a variable

sooo....
array=(Linux Solaris HPUX)
var=$(dialog --backtitle "OS infomration" \
 --radiolist "Select OS:" 10 40 3 \
 1 "Linux 7.2" off \
 2 "Solaris 9" on \
 3 "HPUX 11i" off 3>&1 1>&2 2>&3 3>&- )
printf '\n\nYou chose: %s\n' "${array[$var - 1]}"

0
man dialog

 --stdout
              Direct output to the standard output.  This option is provided
              for compatibility with Xdialog, however using it in portable
              scripts is not recommended, since curses normally writes its
              screen updates to the standard output.  If you use this option,
              dialog attempts to reopen the terminal so it can write to the
              display.  Depending on the platform and your environment, that
              may fail.

therefore:

array=(Linux Solaris HPUX)

opt=$( dialog --stdout \
 --backtitle "OS infomration" \
 --radiolist "Select OS:" 10 40 3 \
 1 "Linux 7.2" off \
 2 "Solaris 9" on \
 3 "HPUX 11i" off )

printf '\n\nYou chose: %s\n' "${array[var - 1]}"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接