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

后端 未结 6 1821
刺人心
刺人心 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:25

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

提交回复
热议问题