Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

whiptail: How to redirect output to environment variable?

I'm trying to use whiptail as it's a lightweight alternative to dialog and seems to be installed by default in most systems (i.e., people don't have to go around and install it if it's "forgotten" or wasn't installed by default). I checked question #1562666 for a few examples here, but I'm looking for an alternative for redirecting output so that is sets an environment variable, instead of just writing to disk.

For example, when I try with dialog, this works (I see the dialog box, and an environment variable is set):

result=$(dialog --output-fd 1 --inputbox "Enter some text" 10 30)
echo Result=$result

However, this doesn't work when using whiptail in place of dialog, as the dialog box never shows up. I have to redirect it to a disk file and read it, for example:

result=$(tempfile) ; chmod go-rw $result
whiptail --inputbox "Enter some text" 10 30 2>$result
echo Result=$(cat $result)
rm $result

It works, and I can use the same tempfile from beginning to end (removing it when the script ends). But it feels awkward to be forced to use the disk just for this, instead of keeping it all in memory (redirecting to an environment variable).

So I'm asking: Am I forgetting something -- or do I really have to use the disk when using whiptail?

Thank you in advance for your feedback.

like image 809
jbatista Avatar asked Dec 28 '09 16:12

jbatista


People also ask

How do you redirect output of a command to a variable?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]

What is Linux whiptail?

Whiptail is a program that allows shell scripts to display dialog boxes to the user for informational purposes, or to get input from the user in a friendly way. Whiptail is included by default on Debian. From the Linux Dictionary: whiptail is a "dialog" replacement using newt instead of ncurses.


2 Answers

This is probably because whiptail uses stdin and stdout to print the input box, so you cannot redirect stderr directly to stdout, but you need to swap them, e.g:

foobar=$(whiptail --inputbox "Enter some text" 10 30 3>&1 1>&2 2>&3)
like image 153
Kimvais Avatar answered Sep 20 '22 14:09

Kimvais


It appears that whiptail(1) writes its control output to the termininal based on the setting of the TERM environment variable. Conseqently, you can't use the standard output stream of whiptail(1) to set a variable. Also, whiptail(1) writes the user-input of the input box to the standard error stream so, again, you can't use its standard output stream to set a variable.

like image 40
Steve Emmerson Avatar answered Sep 20 '22 14:09

Steve Emmerson