2

Is it possible to use sudo -i in a shell script? I think the exit command will run into problems...

#!/bin/bash

sudo -i
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
exit

2 Answers 2

2

Has been tested!... Check how the curl command has been modified...

#!/bin/bash

sudo -u root curl -L "https://github.com/docker/compose/releases/download/1.10.0-rc2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo -u root chmod +x /usr/local/bin/docker-compose
exit
2
  • Then I don't need the exit, right? Commented Jan 18, 2017 at 19:15
  • You don't, if the returning exit status of your script is trivial for you. Keep in mind that without the exit, the exit status will be that of the last command executed.
    – marc
    Commented Jan 18, 2017 at 20:39
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .