I need to copy-paste a bunch of shell commands to emacs and run them as root/sudo. How do I do it?
For instance, while configuring samba, I come across this instruction from the documentation:
Become root:
mkdir /usr/local/samba/lib/usershares chgrp foo /usr/local/samba/lib/usershares chmod 1770 /usr/local/samba/lib/usershares
I would like to paste the commands to a scratch buffer and run. If they don't require sudo, then I can use shell-command-on-region (M-|). But how do I execute these lines as root?
Prefixing sudo to lines fail with
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper.
Adding -S flag to sudo makes the command fail without asking for password.
As an another approch, I switched the *scratch* buffer to shell-script-mode, selected the region and Execute region C-M-x. This works great for non-sudo commands, but fails with the same message as above.
shell-command-on-region. Read the docstring again and you'll understand it can't do what you want. You need to prependsudoto each of the three commands, or start a session as root. You could run these on the scratch buffer, but that's not very orthodox. TryM-x shellinstead.sudorequires providing a password. Got this message: sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper. I couldn't figure out where to give this-Soption.M-x shellgives a more feature-full terminal. But pasting a block of text with multiple commands, especially when they need to be edited before being executed, is tricky. That's why I started with scratch buffer.sudo bash /path/to/filein a shell (in Emacs or in a terminal).