3

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.

7
  • Forget about shell-command-on-region. Read the docstring again and you'll understand it can't do what you want. You need to prepend sudo to 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. Try M-x shell instead. Commented Feb 19, 2022 at 15:06
  • "Prefixing sudo didn't work" - how did it fail? Commented Feb 24, 2022 at 5:54
  • @NickD It didn't work out of the box because sudo requires 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 -S option. Commented Feb 26, 2022 at 13:24
  • @aadcg M-x shell gives 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. Commented Feb 26, 2022 at 13:47
  • So paste them into a buffer, fix them up as needed (the same thing you would do with a scratch buffer IOW), save the buffer into a file, and then execute it with sudo bash /path/to/file in a shell (in Emacs or in a terminal). Commented Feb 26, 2022 at 15:12

1 Answer 1

1

This is one way to copy-paste a bunch of commands to emacs, edit them as needed, and run them all together. Please feel free to add if there's a simpler way to achieve the same result.

  1. Paste the required shell commands to *scratch* buffer. Edit them as required.
  2. Prefix sudo -A to lines that need to be executed as root.
  3. Add export SUDO_ASKPASS=/path/to/your/askpass/program before the shell commands.
    • An askpass program is a helper that will prompt for passwords. On my kde system there is ksshaskpass, I could use `which ksshaskpass` above. It may be different on your system.
    • If SUDO_ASKPASS is already set in the environment, this line is not necessary.
  4. Select the lines and M-x sh-execute-region (or Sh-Script menu -> Execute region C-M-x).
  5. View the result on *Shell Command Output* buffer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.