6

This question relates to another question. If I paste the @kyodake 's suggested code, all at once in the terminal would it work?

Or should I go line by line?

  • @Ravan that's not true. A newline works just as well. Were it not for sudo requiring a password and for the OP of the linked answer including #, the commands could be copy-pasted directly. – terdon Sep 11 '15 at 11:50
8

There's several approaches to letting multiple commands run with sudo.

The first one suits your use-case best. Others are added for the sake of exploring alternatives; they may not suit your particular need, but may come in handy to you in the future or other users.

Approach #1: Cleanest and simplest

Put everything into a script, for example myscript.sh, make the file executable with chmod 755 myscript.sh, and run that script with sudo myscript.sh

Approach #2: long string of commands

You need something with superuser privileges to run multiple commands. That would be shell, ( any shell for that matter, bash or sh ) . Shells have a -c flag, which tells them to run a command or a set of commands separated by a semicolon ;. Simple example: sudo bash -c 'apt-get update; apt-get upgrade'. In particular, kyodake's answer can be re-done like so:

sudo bash -c 'apt-get update;apt-get install --reinstall aptitude deborphan mate-desktop; aptitude remove '?and(?reverse-depends(unity),?not(?reverse-depends(?exact-name(mate-desktop))))';aptitude remove '?and(?reverse-depends(ubuntu),?not(?reverse-depends(?exact-name(mate-desktop))))';aptitude remove '?and(?reverse-depends(gnome),?not(?reverse-depends(?exact-name(mate-desktop))))';apt-get install --reinstall mate-desktop;deborphan;apt-get --purge remove $(deborphan);deborphan --libdevel;apt-get --purge remove $(deborphan --libdevel);deborphan --find-config;dpkg --purge $(deborphan --find-config);apt-get autoremove;apt-get clean;reboot'

Lengthy ? Yes. This approach works better when you need to run just a few commands with sudo.

Approach #3: playing with sudo timeout

By default, the sudo privilege times out after 15 minutes. So if you are certain all those commands won't take that long, you could run the first command with sudo and will be prompted for password; for all the subsequent commands you won't be prompted, but you still need to type sudo.

Approach #4: using root shell

As kyodake suggested himself in the answer, log-in to a root shell with sudo -i to use the root privilege until you exit. Note, that this is not recommended for security reasons. Unless you are 100% certain you will remember to log out once you are done working and no-one will get into your root shell, then... use this approach at your discretion.

  • 3
    "By default, sudo privilege timesout after 15 minutes" - that doesn't mean that all commands will be executed with sudo privilieges for 15 minutes, it only means that you won't have to type password each time you use sudo. – gronostaj Sep 11 '15 at 9:24
  • @gronostaj noted, already edited my answer – Sergiy Kolodyazhnyy Sep 11 '15 at 21:14
1

Copy-pasting the script will work, and the commands will be executed one after the other;

However the problem here are the $ / # characters at the start of each line, which would break each command; removing them is mandatory before running the script in any way.

I think the fastest / most direct way to do this would be to process it and to run it at the same time using the following method:

  • Selecting the script, skipping the first line, as it is (i.e. including the # characters at the start)
  • Copying the script using Ctrl+C
  • Hitting Ctrl+Alt+T
  • Typing sed 's/^.//' <<EOF | sudo bash
  • Hitting Enter
  • Pasting the script using Ctrl+Shift+V
  • Hitting Enter
  • Typing EOF
  • Hitting Enter again

Advantages against creating a script on purpose and running it:

  1. It's quicker
  2. There's no need to create any file
  3. There's no need to process the script to remove the $ / # characters

Here's an example using this script:

# whoami
# echo command1
# echo command2
# echo command3
ubuntu@ubuntu ~ % sed 's/^.//' <<EOF | sudo bash
ubuntu@ubuntu ~ pipe heredoc> # whoami
ubuntu@ubuntu ~ pipe heredoc> # echo command1
ubuntu@ubuntu ~ pipe heredoc> # echo command2
ubuntu@ubuntu ~ pipe heredoc> # echo command3
ubuntu@ubuntu ~ pipe heredoc> EOF
root
command1
command2
command3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.