Inside Emacs it's possible to run a shell command under sudo by doing the following:
(let ((default-directory "/sudo::"))
(shell-command "the-command-that-requires-sudo-access"))
That will prompt for the sudo password and run the command under sudo.
Is it possible to do the same thing with shell-command-on-region
?
I have a filter program that requires sudo access to operate, even if it operates on a file on which I have full user-level control. The program is au-filter
a bash script I wrote. I tried the following but that does not prompt for the sudo password as the first example above does.
(defun my-sudo-auformat ()
"Run au-format under sudo."
(interactive)
(let ((default-directory "/sudo::"))
(shell-command-on-region (region-beginning) (region-end) "au-format")))
Is this expected? Should I use some other process function instead of shell-command-on-region
for this purpose? From what I see from this SO Q/A , shell-command-on-region
does not support Tramp remote execution. Does this mean that unless shell-command-on-region
is modified my second function cannot work properly and all I can do is use a modification of the first example to process a complete file and not some lines of a buffer in this case?
And if that's the case, is there any other existing function that could be used to achieve what I'd like to do?
Edit - Clarification: I am looking for away to do it completely inside of Emacs; ie. without the need for an external program that will pop an UI/GUI to prompt for the sudo password. The sudo prompt would show up inside on buffer of my Emacs session.
A method I would be able to use:
- when I edit files on my local host running a program locally and,
- edit files on a remote host and execute the program running on the remote host. In the scenario 2 I could ssh to the remote host and run Emacs on that remote host.
If there was a way to use Emacs on the local host and use Tramp to process the remote files using the remote program that would be a bonus but it's not the focus of this question.
C-u M-|
for that. The au-format script needs to execute ausearch to perform some reformatting. That requires access to the /var/log/audit/audit.log file, which requires sudo access. I have work-arounds. I'm just trying to automate this.