shell scripting that uses sudo su - team and perform rest command under this sudo access only
Question
#!/bin/bash
sudo su - team //// this will change user to team
<somecommnand>
here i have some command like sqlplus that run under this privilege only .
but ,this script first asked for password then i give password but it not run rest of command like .
How to write script that use sudo su and run all command in script below it.
La solution
You could try having all of the commands on the same line and seperate them with ';'.
eg. 'sudo su; somecommand; another command'
Could also run that script as the intended user.
Autres conseils
You have several options.
You can just move the
sudo
command outside of your script, and have people call it like this:sudo -u team /path/to/your/script
You can put the
sudo
command in one script and the actual commands you want to run with elevated privileges in another script. That is, people runyour_script
, which looks like:#!/bin/sh sudo -u team /path/to/your_script.real
And
your_script.real
contains the commands that you want to run with elevated privileges.If you really want everything in the same script and you don't mind being a little too clever, you can have the script check re-execute itself using
sudo
like this:#!/bin/sh TEAM_UID=500 # replace with correct uid if [ "$UID" != $TEAM_UID ]; then exec sudo -u team $0 fi
And you can also feed commands to a shell from stdin, like this:
#!/bin/sh sudo su - team <<'EOF' echo my uid is $UID EOF