3

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.

10

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.