我可以在 bash 脚本中间运行 'su' 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3420280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can I run 'su' in the middle of a bash script?
提问by Radek
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
回答by
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
)。
回答by David Braun
You can use a here documentto embed multiple su
commands in your script:
您可以使用此处的文档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
回答by mvds
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.
command
将被执行user
- 如果su
成功,通常只有无密码用户或以 root 身份运行脚本时才会出现这种情况。
Use sudo
for a better and more fine-grained approach.
使用sudo
一个更好和更精细的方法。
回答by Wolph
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'
回答by Viswa Teja Kuncham
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?
回答by RaamEE
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"
我正在以“root”身份运行脚本“my_script”并希望脚本以用户“raamee”身份运行
#!/bin/bash
#Script name is: my_script
user=`whoami`
if [ "$user" == "root" ]; then
# As suggested by glenn Hymanman. 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