bash 脚本:如何使用对话框获取无线电列表上的项目名称

发布于 2024-12-26 05:07:34 字数 280 浏览 0 评论 0原文

我需要使用对话框界面在 bash 脚本中创建一个无线电列表,例如,如果我有以下列表:

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

我需要当用户选择一个选项并按“确定”时,我的脚本可以读取项目的名称而不是项目的编号。

有可能吗?

谢谢!

I need to make a radiolist in bash script using dialog interface, for example if I have the following list:

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

I need when the user chooses an option and presses "ok", my script could read the item's name and not the item's number.

It is possible?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无法回应 2025-01-02 05:07:34

您可以将预期结果放入数组中:

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]}"

You can put your expected results in an array:

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]}"
各自安好 2025-01-02 05:07:34

Termux 应用 > 中/dev/tty 2>&1 不起作用,但 3>&1 1>&2 2>&3 3>&- 工作正常。


这意味着:3>&1打开一个指向stdout的新文件描述符,1>&2将stdout重定向到stderr,2> &3 将 stderr 指向 stdout,并且 3>&- 在命令执行后删除文件描述符 3

取自:BASH:变量中的对话框输入



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]}"

in Termux app >/dev/tty 2>&1 is not working, but 3>&1 1>&2 2>&3 3>&- is working perfectly.


which means: 3>&1 opens a new file descriptor which points to stdout, 1>&2 redirects stdout to stderr, 2>&3 points stderr to stdout and 3>&- deletes the files descriptor 3 after the command has been executed.

taken from: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]}"
美人如玉 2025-01-02 05:07:34
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.

因此:

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]}"
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]}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文