我需要使用对话框界面在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
当用户选择一个选项并按下“确定”时,我希望我的脚本能够读取该项的名称而不是项号。
这是可能的吗?
谢谢!
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]}"
>/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。
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]}"
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]}"