BASH:变量中的对话框输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29222633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BASH: Dialog input in a variable
提问by Ali Sajid
I am trying to get user input from the inputbox using the dialog
command to create a directory in a bash script. Currently I have the following code:
我正在尝试使用dialog
命令从输入框获取用户输入以在 bash 脚本中创建目录。目前我有以下代码:
rm -f ans.txt
dialog --title "Create Directory" --inputbox "Enter the directory name:" 8 40 2>ans.txt
val=$(<ans.txt)
mkdir $val
It works, however it requires the creation (and deletion) of a temporary file. Can I store the user input from dialog
directly into $val
without using a temporary file?
它可以工作,但是它需要创建(和删除)临时文件。我dialog
可以在$val
不使用临时文件的情况下将用户输入直接存储到其中吗?
采纳答案by hek2mgl
Basically you will use command substitution to obtain the output of a command into a variable. Like this:
基本上,您将使用命令替换将命令的输出获取到变量中。像这样:
date=$(date)
which writes the output of the date
command to the variable $date
.
它将date
命令的输出写入变量$date
。
But if we try the same with the dialog dialog
command:
但是,如果我们对 dialogdialog
命令尝试相同的操作:
user_input=$(dialog --title "Create Directory" --inputbox "Enter the directory name:" 8 40)
we get an empty screen! Why does this happen?
我们得到一个空屏幕!为什么会发生这种情况?
Explanation:
解释:
dialog
outputs the user input on stderr, since stdout will already being used by ncurses to update the screen. Without output redirection being used, the command substitution will return the command's stdout into the variable - meaning the output of ncurses will not get printed on screen. However if you type something (you can't see anything while typing):
dialog
在 stderr 上输出用户输入,因为 ncurses 已经使用 stdout 来更新屏幕。如果不使用输出重定向,命令替换会将命令的标准输出返回到变量中——这意味着 ncurses 的输出不会打印在屏幕上。但是,如果您键入某些内容(键入时看不到任何内容):
test<enter>
The text test
will appear on screen. This happens because it will been written to stderr and stderr still points to the current terminal.
文本test
将出现在屏幕上。发生这种情况是因为它将被写入 stderr 并且 stderr 仍然指向当前终端。
Note:You might expect the ncurses output in $user_input
but $user_input
is empty after the command. I assume that this happens because dialog
will check if it's output is going to a tty and it won't output anything otherwise. (Haven't verfied that)
注意:您可能期望 ncurses 输出在$user_input
但$user_input
在命令之后为空。我认为发生这种情况是因为dialog
会检查它的输出是否会发送到 tty,否则不会输出任何内容。(没有验证过)
Solution:
解决方案:
We can exchange stderr and stdout using i/o rerouting. We will turn stderr into stdout and write the user input to the variable but on the other hand turn stdout into stderr which will make ncurses outputting to the screen:
我们可以使用 i/o 重新路由交换 stderr 和 stdout。我们将 stderr 转换为 stdout 并将用户输入写入变量,但另一方面将 stdout 转换为 stderr,这将使 ncurses 输出到屏幕:
3>&1 1>&2 2>&3 3>&-
In short: 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.
简而言之:3>&1
打开一个指向stdout的新文件描述符,将stdout1>&2
重定向到stderr,将stderr2>&3
指向stdout并在命令执行后3>&-
删除文件描述符3
。
This gives us the final command:
这给了我们最后的命令:
user_input=$(\
dialog --title "Create Directory" \
--inputbox "Enter the directory name:" 8 40 \
3>&1 1>&2 2>&3 3>&- \
)
mkdir "$user_input"
回答by chimay
Note that Zenity is a GUI alternative.
请注意,Zenity 是一种 GUI 替代方案。
If you looking for a text interface, fzf is really nice. You can easily redirect its output :
如果您正在寻找文本界面,fzf 非常好。您可以轻松重定向其输出:
var=$(print -l folder/* | fzf)
If you just want to read user input :
如果您只想阅读用户输入:
var=$(echo '' | fzf --print-query)
It's zsh syntax, bash users will translate it easily I suppose.
这是 zsh 语法,我想 bash 用户会很容易翻译它。
Also, fzf interacts well with tmux.
此外,fzf 与 tmux 交互良好。