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?
There's several approaches to letting multiple commands run with
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
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,
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
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.
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:
#characters at the start)
sed 's/^.//' <<EOF | sudo bash
Advantages against creating a script on purpose and running it:
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