passing contents to EDITOR and getting the results after quit (e.g. git/svn commit)
I would like to开发者_运维问答 write a command line tool that passes some formatted text to whatever EDITOR the user has set in the environment and then reading the contents back.
How do tools like svn commit and git commit handle this behavior? Is there a standard pattern for doing this?
hmm, maybe...
- Create a temporary file with the content you want.
- Open the file in the $EDITOR.
- Wait until the editor was closed
- Check the modification date of the file
- unchanged: The user has probably aborted the editing process
- changed: use the content of the modified file
- Delete the temporary file
If you have a question to one of these tasks, feel free to ask :)
#!/bin/bash
cat | $EDITOR - &
wait $!
echo "now editor has exited"
精彩评论