通过打开包含内容的文本编辑器来捕获用户输入 [英] Capture user input by opening a text editor with content
问题描述
我想从bash脚本中
- 为当前用户打开默认文本编辑器
- 在其中粘贴字符串
$ original_content
- 一旦用户修改了内容,然后关闭了文本编辑器,
- 将修改后的字符串捕获到变量
$ modified_content
- 然后保存
$修改后的内容
到$ output_file
- Open the default text editor for current user
- Paste a string
$original_content
in it - Once the user modifies the content then closes the text editor,
- Capture the modified string into a variable
$modified_content
- Then save
$modified_content
to an$output_file
Google搜索用于捕获用户输入的内容显示 read
,这不是我想要的内容。
Google searches for capturing user input shows read
which is not what I'm looking for.
有人可以将我指向
谢谢
推荐答案
该方法有望成功对于大多数编辑者:
This method should hopefully work for most editors:
#!/bin/bash
original_content="Your original content"
echo $original_content > /tmp/user_input.tmp
# For example:
# DEFAULT_EDITOR=/usr/bin/vi
$DEFAULT_EDITOR /tmp/user_input.tmp
modified_content=`cat /tmp/user_input.tmp`
echo $modified_content > /tmp/output_file
该脚本可能有点用处,但它执行了您想要的所有操作,除了对于粘贴部分,因为您可能必须适应所有类型的编辑器才能正确地粘贴字符串。该脚本利用了这样的好处:以文件名作为参数调用大多数编辑器会打开该文件进行编辑,从而将 $ original_content
粘贴到编辑器中。
This script may be a little drawn out but it performs all the actions you wanted except for the pasting part, since you'd probably have to accommodate for all varieties of editors to properly "paste" a string. This script utilizes the benefit that calling most editors with a filename as a parameter opens that file for editing thereby "pasting" your $original_content
in the editor.
这篇关于通过打开包含内容的文本编辑器来捕获用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!