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

后端 未结 6 1813
刺人心
刺人心 2021-02-04 23:43

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         


        
6条回答
  •  不思量自难忘°
    2021-02-05 00:16

    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.

提交回复
热议问题