5

I would like to write a bash script to perform some tasks under root while logging as normal user. Is it possible to have something like this?

#!/usr/bash

sudo -i

<multiple line perform all my sudo command>
...
...
...
exit

<back to my user command, continue some other task>

Of course I can put a sudo every line, but I would like to find out is it possible to do something like above? Thanks.

2 Answers 2

11

How about something simple like this.

#!/usr/bash
sudo bash << EOF
# Everything until EOF is run in a sub-shell
echo 'Hello World'
whoami
EOF
3

Here is a simpler approach:

#!/usr/bash

sudo sh another_script.sh

<continue some other task>

Here is the other script.

#!/usr/bash
<multiple line perform all my sudo command>
...
...
...

If you want to avoid prefixing each command with sudo, you can just include them in another script and call it.

1
  • This will work, but you don't really need a separate file, just a sub-shell.
    – Zoredache
    Feb 19, 2012 at 8:23

You must log in to answer this question.

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