9

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!

3 Answers 3

4

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]}"
Sign up to request clarification or add additional context in comments.

4 Comments

I thought something similar, but there isn't a direct solution? Like for example when you use an inpunt box, where the inserted value is stored on stderr?
There might be, but I do not know of one. This might be your closest solution. Someone else may chime in with an answer.
This is the answer I was looking for - one caveat, the redirection syntax is backwards (common script error), it should be >/dev/tty 2>&1 I think? gnu.org/software/bash/manual/bashref.html#Redirections
@synthesizerpatel - you are right, stderr wouldn't have gone to the tty. Post edited.
0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.