You can't use sudo
in that way. The script would give you an interactive root shell and pause until you exited the shell. Then it would run the remaining commands in the script without root
privileges.
If your script only does things that requires root
privileges, then run the whole script with sudo
:
$ sudo ./myscript.sh
The script would look like
#!/bin/bash
curl -L https://github.com/docker/compose/releases/download/1.10.0-rc2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
(Note: I haven't actually looked to see what these commands actually do)
Otherwise, and this is just a personal opinion, it may be a good idea to partition out the root
bits out of the script and put them in scripts of their own, at least if it's common tasks.
#!/bin/bash
# script that does things as an ordinary user
# ...
sudo "$HOME/admin_tasks/do_docker_stuff.sudo"
# rest of script
# ...
Or, you could just do what everybody else seems to be doing and prefix every line in the script with sudo
. In this case it would fail though since there's a redirection on the first line that needs root
privileges, so you "fix it" by using sudo bash -c " ... > ... "
instead, turning the nice and readable script into a mess of confusion (again, just a personal opinion).
After thinking a bit: This isn't a long script, so doing multiple sudo
calls in the script might not be such a bad idea, but we can leave the invocation of curl
outside of sudo
:
#!/bin/bash
curl -L -o ./docker-compose "https://github.com/docker/compose/releases/download/1.10.0-rc2/docker-compose-$(uname -s)-$(uname -m)"
chmod a+x ./docker-compose
sudo mv ./docker-compose /usr/local/bin/docker-compose
sudo chown root:root /usr/local/bin/docker-compose
Those last three command could be put into a do_local-bin_install.sudo
script (for re-use by other scripts):
#!/bin/bash
binary="$1"
chmod a+x "$binary"
chown root:root "$binary"
mv -i "$binary" /usr/local/bin/
which we could call from our script:
#!/bin/bash
curl -L -o ./docker-compose "https://github.com/docker/compose/releases/download/1.10.0-rc2/docker-compose-$(uname -s)-$(uname -m)"
sudo ./do_local-bin_install.sudo ./docker-compose
Sorry, this was a bit of an stream of consciousness type of answer. I hope someone finds it comprehensible.