I want to have an input mechanism akin to git commit. When the script is run, it should open an editor to edit a temporary file, use this file as my input, and then get rid of the file.
2 Answers
One solution is with the the builtin read command.
read -r -p 'Please enter your favorite editor' editor
echo "You have enter $editor"
Now the value is in the $editor" variable, next step would be to test if there is indeed an editor that is installed or within your PATH.
if type "$editor" >/dev/null 2>&1; then
command "$editor" ENTER_THE_FILE_YOU_WANT_TO_EDIT
else
echo "$editor no such file or directory!" >&2
exit 127
fi
see
help type
help command
The 127 exit status is what the shell will exit if there is no executable, alias, function. have a look at
man 1p exit
You can use vipe with nothing (or some default text) as input:
% foo=$(echo | vipe)
# opens an editor, I add `foo bar`
% echo "$foo"
foo bar
vipe will take care of creating the temporary file and deleting it.