![]() I’d appreciate some experienced eyes on this to validate it, as it’s my first try at what I believe is a novel solution. The process, while seemingly complicated, can pretty much be handled in a one-liner.Īnd can definitely be scripted for reusability if you find yourself doing it often. In your editor, save the file to ~/forgotsudo.oops, which will append the context to the original file as sudo.Run f=forgotsudo.oops cd ~ || mkfifo $f sudo sh -c "cat $f > " rm $f.Short answer (but please read the details): (But I think a full implementation of this program would make Vi’s view redundant.) There’s already a program called view that’s part of Vi/Vim, so feel free to suggest a better name. usr/local/bin/view (or ~/bin/view) editor=nano Fix the permissions or run "view" instead." "$1" 1>&2Īnd a command I called view so that you can avoid the prompt if you know you aren’t going to make any changes. usr/local/bin/edit (or ~/bin/edit) sudo= # empty is false, non-empty is trueĮditor=nano # XXX check $EDITOR and $VISUAL It checks if you can edit the file, and if you can’t, it runs “nano” as root instead. ( UPDATE: Apparently nano 2.2 does warn 2.0 doesn’t.) I just tried nano, and what I found most surprising is it doesn’t even warn you that the file is read-only when you start trying to edit the file. Nano does not have the ability to launch a new process or pass data to other processes, so it’s left out of this party. See for example this related question for other solutions in advanced editors that allow writing the file buffer to a process pipe. ¹ Some editors are actually able to do this by launching a new process with different permissions and passing the data off to that process for saving. Background the editor with Ctrl+ z, change the file ownership or permissions so you can write to it, then use fg to get back to the editor and save.Note that it is not recomended to use mv for this because of the change in file ownership and permissions it is likely to cause, you just want to replace the file content not the file placeholder itself. Save to a temporary file in /tmp or wherever, close the editor, then dump the contents of temp file into the file you were editing.No, you can’t give a running program permissions that it doesn’t have when it starts, that would be the security hole known as ‘privilege escalation’¹. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |