SyntaxErr0r
SyntaxErr0r

Reputation: 76

How to execute multiple bash commands in a root sub shell?

I have a shell script that executes a series of terminal commands. What I would like to do is have these commands run through a root shell as they need root privileges.

So far:

Manually I would type :

sudo bash

into the terminal and a root shell would appear and I can execute all my commands.

Problem:

When I try to automate this process in my script, the root shell appears but none of my commands execute, once I close the root shell with exit the commands then execute.

How can I fix this problem?

Upvotes: 0

Views: 2835

Answers (4)

Jahid
Jahid

Reputation: 22428

There are various ways for a custom script to execute all your commands with superuser:

Using heredoc:

sudo -s <<EOF
#all root commands
#here
EOF

Using sudo in every command:

sudo cmd1
sudo cmd2
...

Compared to the heredoc method it has one disadvantage: If any command takes much time that the sudo session expires then the next sudo will ask for password again.

running the script with sudo will run all of it's commands with superuser privilege.

sudo /path/to/myscript
#all commands inside myscript gets sudo privilege

Upvotes: 2

sjsam
sjsam

Reputation: 21965

Regarding :

When I try to automate this process in my script, the root shell appears but none of my commands execute, once I close the root shell with exit the commands then execute.

This is because doing sudo bash spawns a sub-shell which even though has the root privileges will be useless because the commands that you wish to execute are in the parent shell. So once you exit this root shell your successive commands run in the normal shell with normal privileges.

You could do it this way :

#!/bin/bash
function do_on_demand()
{
#The commands that you wish to execute as root goes here.
}
export -f do_on_demand
su root -c "do_on_demand"

Save as say supercow, do chmod u+x supercow and run it.

Doing this you could run a set of commands as root just by entering the root password once.

Edit:

This may give you the root subshell as you wish :

su - -c "bash -c 'command1;command2'"

But a problem with this one is that the parent environment can't be used here, so be vigilant to give the full path to your scripts inside the bash commands.

Upvotes: 1

user3606374
user3606374

Reputation: 9

In my scripts, I just type sudo before any command that needs higher privilege. You can do sudo su as the first command in your script. When you run the script, it will ask for your password, and then complete as necessary.

Upvotes: 0

uml&#228;ute
uml&#228;ute

Reputation: 31284

sudo can run any command as root, not just a bash. actually, it's suggested use is to run the commands you need rather than open a generic shell with root access.

so usually, you find something like the following in a script, giving super-user powers to only the commands that require them:

 $ cat myscript
 # cmd1 doesn't require supercow powers
 cmd1
 # but cmd2 and cmd3 do
 sudo cmd2
 sudo cmd3
 # cmd4 doesn't require supercow either
 cmd4
 $ ./myscript
 $

the nice part is, that sudo will cache the authorization for you: so if you have just entered the password correctly for cmd2, it won't ask you again for cmd3.

finally, if this is much to tedious you could also run the entire script via sudo (though again it's better to keep superuser privileges to a bare minimum, so i wouldn't exactly recommend this):

 $ cat myscript
 cmd1
 cmd2
 cmd3
 cmd4
 $ sudo ./myscript
 $

Upvotes: 4

Related Questions