0

I have an application which does this:

#!/bin/bash
tmpfile=$(mktemp)
gedit $tmpfile && pkexec mv $tmpfile $1

Works great. It creates a temp file, opens it in a graphical editor, and it gets moved to a correct spot with elevated permissions using polkit when I'm done.

But... you need to have gedit installed. I'd prefer to use something which respects the user's desktop environment and preferences. xdg-open should work but I have a problem:

#!/bin/bash
tmpfile=$(mktemp)
xdg-open $tmpfile && pkexec mv $tmpfile $1

xdg-open is a forking process. It will launch the editor, then return immediately... causing the mv to be triggered before I've had a chance to work.

Is there a way for me to only trigger the pkexec mv ... after the editor completes?

xdg-open(1) doesn't offer any help. Am I going to need to write some kind of inotify tool to block until some event is read from an FD?

Stewart
  • 13,677
  • 1
    This looks like you’re reinventing sudoedit, but the latter’s UX might not match your expectations. – Stephen Kitt Apr 21 '20 at 15:35
  • I'm actually doing this from C in an un-privilaged GUI app. I'm launching processes in the manner shown in bash (so it's honestly not a bash-specific question). Instead of writing my own editor, I thought it would be best to use the default. Also, since my app isn't elevated I'm relying on pkexec. sudo and sudoedit don't really work well from inside of an unprivilaged GUI. Actually, writing to a tempfile with the user's editor, then moving it was an idea I got from visudo. – Stewart Apr 21 '20 at 15:40
  • 1
    Right, I get that (hence my comment about the UX). Your question is really about waiting for an edit operation to complete, and that’s rather difficult. Try opening one file in gedit, then running your first example — the temporary file will open in the running editor and your pkexec will proceed without waiting. Some editors can be asked to wait (see here), but xdg-open doesn’t have anything equivalent. – Stephen Kitt Apr 21 '20 at 15:45
  • 1
    You could rely on mimeopen -n instead – A.B Apr 22 '20 at 02:23
  • @A.B Yes, this works! I was about halfway through implementing inotify when I saw your answer. – Stewart Apr 22 '20 at 09:42
  • 1
    Watch out, mimeopen doesn’t necessarily wait either, it doesn’t do anything special; on my system, opening with gedit using mimeopen doesn’t wait if the editor is already running. – Stephen Kitt Apr 22 '20 at 14:52

0 Answers0