Capture user input by opening a text editor with content

半腔热情 提交于 2020-01-05 05:52:30

问题


From a bash script, I'd like to

  1. Open the default text editor for current user
  2. Paste a string $original_content in it
  3. Once the user modifies the content then closes the text editor,
  4. Capture the modified string into a variable $modified_content
  5. Then save $modified_content to an $output_file

Google searches for capturing user input shows read which is not what I'm looking for.

Can someone point me to the right direction?

Thank you


回答1:


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

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.



来源:https://stackoverflow.com/questions/7375434/capture-user-input-by-opening-a-text-editor-with-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!