我可以在bash脚本中运行'su'吗?

时间:2021-10-19 01:10:58

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

我可以在脚本中间更改/ su用户吗?

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

6 个解决方案

#1


40  

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

你可以,但bash不会以postgres的形式运行后续命令。相反,做:

su postgres -c 'dropdb $user'

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

-c标志以用户身份运行命令(请参阅man su)。

#2


17  

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

您可以使用here文档在脚本中嵌入多个su命令:

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

#3


10  

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

不是这样的。 su将调用一个默认为shell的进程。在命令行中,此shell将是交互式的,因此您可以输入命令。在脚本的上下文中,shell将立即结束(因为它无关)。

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.

命令将以用户身份执行 - 如果su成功,通常只有无密码用户或以root身份运行脚本的情况。

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

使用sudo可以获得更好,更细粒度的方法。

#4


4  

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.

不,你不能。或者至少......你可以su,但su只会在那个时候打开一个新shell,当它完成时它会继续执行脚本的其余部分。

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

解决它的一种方法是使用su -c'some command'

#5


2  

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:

我今天听到的另一个有趣的想法是对脚本进行递归调用,当你以root身份运行并且想要以另一个用户身份运行脚本时。请参阅以下示例:

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

我正在运行脚本“my_script”作为“root”,并希望脚本以“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

#6


1  

Refer to answers in below question,

请参阅以下问题的答案,

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

您可以在答案中提到的<< EOF和EOF之间书写。

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

How do I use su to execute the rest of the bash script as that user?

如何使用su来执行该用户的其余bash脚本?

#1


40  

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

你可以,但bash不会以postgres的形式运行后续命令。相反,做:

su postgres -c 'dropdb $user'

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

-c标志以用户身份运行命令(请参阅man su)。

#2


17  

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

您可以使用here文档在脚本中嵌入多个su命令:

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

#3


10  

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

不是这样的。 su将调用一个默认为shell的进程。在命令行中,此shell将是交互式的,因此您可以输入命令。在脚本的上下文中,shell将立即结束(因为它无关)。

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.

命令将以用户身份执行 - 如果su成功,通常只有无密码用户或以root身份运行脚本的情况。

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

使用sudo可以获得更好,更细粒度的方法。

#4


4  

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.

不,你不能。或者至少......你可以su,但su只会在那个时候打开一个新shell,当它完成时它会继续执行脚本的其余部分。

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

解决它的一种方法是使用su -c'some command'

#5


2  

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:

我今天听到的另一个有趣的想法是对脚本进行递归调用,当你以root身份运行并且想要以另一个用户身份运行脚本时。请参阅以下示例:

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

我正在运行脚本“my_script”作为“root”,并希望脚本以“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

#6


1  

Refer to answers in below question,

请参阅以下问题的答案,

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

您可以在答案中提到的<< EOF和EOF之间书写。

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

How do I use su to execute the rest of the bash script as that user?

如何使用su来执行该用户的其余bash脚本?