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 pos
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.