gpt4 book ai didi

ruby - 如何让 'system("source ~/.bash_profile")' 与当前 shell 一起工作?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:54:19 24 4
gpt4 key购买 nike

我正在以编程方式更新 bash 文件。在脚本的末尾,我调用了 system("source ~/.bash_profile") 来获取 bash 文件。但是,bash 文件没有在当前 shell 中更新。

经过一些研究,我发现 system() 在子 shell 中执行。因此(我认为)当前 shell 未使用最新的 bash 文件更新。

似乎其他调用shell命令的方法也不会在当前shell中执行它们。即反引号、execopen3open4

如何在不打开新 shell 的情况下调用将更新当前 shell 的 shell 命令(以中断当前工作流程)?

我正在查看这些命令,包括

source ~/.bash_profile

alias something="echo something"

更新:建议的答案有效。因此,我调用 system("kill -SIGUSR1 #{Process.ppid}") 获取 .bash_profile。请注意,如果您使用的是 Mac,则应将建议的代码放在 .bash_profile 而不是 .bashrc

最佳答案

在你的 .bashrc.bash_profile 中:

sigusr1() {
source ~/.bash_profile
echo 'wow! I feel refreshed!'
}

trap sigusr1 SIGUSR1

现在,从您的程序中找出 bash pid(可能只是 getppid())并使用 kill() 将 SIGUSR1 发送给它。

如果拍错了过程,你会玩得很开心!这是建立新联系的某种方式,尤其是对于那些刚刚终止进程的人。

关于ruby - 如何让 'system("source ~/.bash_profile")' 与当前 shell 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27731406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com