简体   繁体   English

在 bash 中 exec 后继续执行脚本

[英]Continue script execution after exec in bash

I have such code:我有这样的代码:

echo 'test'
usr=$USER
sudo sh -c "exec su $usr"
echo 'test1'

And for example echo 'test' is code where I configuring something that requirs me to relogin in new shell.例如echo 'test'是我配置一些需要我在新 shell 中重新登录的代码。 But then where code is echo 'test1' I need to continue configuring using reloaded new shell.但是代码是echo 'test1'我需要继续使用重新加载的新 shell 进行配置。

Is there way to do that automatically?有没有办法自动做到这一点? Like start new shell in parallel or something like that?像并行启动新外壳或类似的东西?

Ubuntu 14.04, bash. Ubuntu 14.04,bash。

Update:更新:

For example, I need to install virsh but after installation it requires sudo to run.例如,我需要安装 virsh 但安装后它需要 sudo 才能运行。 My script configurs groups adding $USER to the libvirtd group.我的脚本配置组将 $USER 添加到 libvirtd 组。 Then I need to relogin.然后我需要重新登录。 I can do that with sudo sh -c "exec su $usr" .我可以用sudo sh -c "exec su $usr"做到这一点。 After that I need the script to continue execution.之后我需要脚本继续执行。 Is there way to do that?有没有办法做到这一点?

Change with visudo your sudoers configuration so that you are no longer asked for a password.使用visudo更改您的 sudoers 配置,以便不再要求您输入密码。 See man visudo and man sudoers .请参阅man visudoman sudoers

I suggest to use a heredoc :我建议使用heredoc

echo 'test'
usr="$USER"
sudo su - "$USER" << EOF
echo 'test1'
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM