[英]Can I run 'su' in the middle of a bash script?
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
You can, but bash won't run the subsequent commands as postgres.您可以,但 bash 不会将后续命令作为 postgres 运行。 Instead, do:相反,请执行以下操作:
su postgres -c 'dropdb $user'
The -c
flag runs a command as the user (see man su
). -c
标志以用户身份运行命令(请参阅man su
)。
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
Not like this.不是这样的。 su
will invoke a process, which defaults to a shell. su
将调用一个默认为 shell 的进程。 On the command line, this shell will be interactive, so you can enter commands.在命令行上,此 shell 将是交互式的,因此您可以输入命令。 In the context of a script, the shell will end right away (because it has nothing to do).在脚本的上下文中,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
以获得更好、更细粒度的方法。
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'
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 脚本的其余部分?
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.我今天听到的另一个有趣的想法是对脚本进行递归调用,当您以 root 身份运行并且希望以其他用户身份运行脚本时。 See the example below:请参阅下面的示例:
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 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.