Whittail:如何将输出重定向到环境变量?

发布于 2024-08-16 01:42:57 字数 954 浏览 5 评论 0原文

我正在尝试使用 whiptail 因为它是一个轻量级dialog 的替代方案,似乎默认安装在大多数系统(即,如果它被“忘记”或默认情况下未安装,人们不必到处安装它)。 我在这里检查了问题#1562666中的一些示例,但我正在寻找重定向输出的替代方法,以便设置一个环境变量,而不仅仅是写入磁盘。

例如,当我尝试使用对话框时,这是有效的(我看到了对话框,并且设置了环境变量):

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

但是,当使用鞭尾代替对话框时,这不起作用,因为对话框永远不会显示。我必须将其重定向到磁盘文件并读取它,例如:

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

它可以工作,我可以从头到尾使用相同的临时文件(在脚本结束时删除它)。但仅仅为此而被迫使用磁盘,而不是将其全部保存在内存中(重定向到环境变量),感觉很尴尬。

所以我问:我是否忘记了什么——或者我在使用whiptail时真的必须使用磁盘吗?

预先感谢您的反馈。

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.

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

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

发布评论

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

评论(2

久隐师 2024-08-23 01:42:57

这可能是因为whiptail使用stdin和stdout来打印输入框,所以你不能将stderr直接重定向到stdout,但你需要交换它们,例如:

foobar=$(whiptail --inputbox "Enter some text" 10 30 3>&1 1>&2 2>&3)

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)
幼儿园老大 2024-08-23 01:42:57

看起来,whiptail(1) 根据 TERM 环境变量的设置将其控制输出写入终端。因此,您不能使用whiptail(1) 的标准输出流来设置变量。此外,whiptail(1) 将输入框的用户输入写入标准错误流,因此您不能使用其标准输出流来设置变量。

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.

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