Can I run 'su' in the middle of a bash script?

BashShell

Bash Problem Overview


Can I change/su user in the middle of a script?

if [ "$user" == "" ]; then
  echo "Enter the table name";
  read user
fi

gunzip *
chown postgres *
su postgres 
dropdb $user
psql -c "create database $user with encoding 'unicode';" -U dbname template1
psql -d $user -f *.sql

Bash Solutions


Solution 1 - Bash

You can, but bash won't run the subsequent commands as postgres. Instead, do:

su postgres -c 'dropdb $user'

The -c flag runs a command as the user (see man su).

Solution 2 - Bash

You can use a here document to embed multiple su commands in your script:

if [ "$user" == "" ]; then
  echo "Enter the table name";
  read user
fi

gunzip *
chown postgres *
su postgres <<EOSU
dropdb $user
psql -c "create database $user with encoding 'unicode';" -U dbname template1
psql -d $user -f *.sql
EOSU

Solution 3 - Bash

Not like this. su will invoke a process, which defaults to a shell. On the command line, this shell will be interactive, so you can enter commands. In the context of a script, the shell will end right away (because it has nothing to do).

With

su user -c command

command will be executed as user - if the su succeeds, which is generally only the case with password-less users or when running the script as root.

Use sudo for a better and more fine-grained approach.

Solution 4 - Bash

Refer to answers in below question,

You can write between << EOF and EOF as mentioned in answers.

#!/bin/bash
whoami
sudo -u someuser bash << EOF
echo "In"
whoami
EOF
echo "Out"
whoami

https://stackoverflow.com/questions/1988249/how-do-i-use-su-to-execute-the-rest-of-the-bash-script-as-that-user

Solution 5 - Bash

No you can't. Or atleast... you can su but su will simply open a new shell at that point and when it's done it will continue with the rest of the script.

One way around it is to use su -c 'some command'

Solution 6 - Bash

Another interesting idea that I heard today is to do a recursive call on the script, when you run as root and you want to run the script as another user. See the example below:

I am running script "my_script" as "root" and want the script to run as user "raamee"


#!/bin/bash

#Script name is: my_script

user=`whoami`

if [ "$user" == "root" ]; then
  # As suggested by glenn jackman. Since I don't have anything to run once 
  # switching the user, I can modify the next line to: 
  # exec sudo -u raamee my_script and reuse the same process
  sudo -u raamee my_script
fi

if [ "$user" == "raamee" ]; then
  #put here the commands you want to perform
  do_command_1
  do_command_2
  do_command_3
fi

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRadekView Question on Stackoverflow
Solution 1 - Bashuser113292View Answer on Stackoverflow
Solution 2 - BashDavid BraunView Answer on Stackoverflow
Solution 3 - BashmvdsView Answer on Stackoverflow
Solution 4 - BashViswa Teja KunchamView Answer on Stackoverflow
Solution 5 - BashWolphView Answer on Stackoverflow
Solution 6 - BashRaamEEView Answer on Stackoverflow