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.

Was it helpful?

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.

OTHER TIPS

You have several options.

  1. 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
    
  2. 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 run your_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.

  3. 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
    
  4. 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
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top