Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should bash scripts use sudo, or assume sudo?

Which pattern is preferable:

#!/bin/bash
echo Installing blah
apt-get install -y blah

...which will fail if run without root perms, or:

#!/bin/bash
echo Installing blah
sudo apt-get install -y blah

...which will succeed as long as the user has sudo access.

I have tended to use the second pattern, but it seems to be rare, so I'm asking what its disadvantages are. The benefits I see are:

  • It's clear which commands actually require superuser permissions to run (useful if the reader wants to pull the script apart)
  • Saves a few keystrokes for the user.

I guess downsides include that the use of root permissions might be surprising ("I didn't type sudo, so I didn't expect anything like apt-get to be run...."). What else?

like image 595
Steve Bennett Avatar asked Jul 25 '14 05:07

Steve Bennett


1 Answers

I will prefer second Pattern, because running few commands with sudo is better rather then running entire script with root permissions in which some commands are needless to have root access and so if you perform these commands with root user further using that command outputs will again need root access or you will have to change the ownership. So I will prefer second approach where sudo is necessary do it, else go local user.

like image 62
slashRahul Avatar answered Sep 25 '22 20:09

slashRahul