Python“源HOME/.bashrc”与 os.system()

发布于 2024-09-18 01:46:28 字数 482 浏览 4 评论 0原文

我正在编写一个 python 脚本(Linux),它添加一些 shell 别名 (将它们写入 HOME/.bash_aliases)。

为了使别名在编写后立即可用,我应该发出以下 bash 内置命令:

source HOME/.bashrc

source 是 bash 内置命令,所以我不能只是:

os.system(source HOME/.bashrc)

如果我尝试类似的操作:

os.system('/bin/bash -c source HOME/.bashrc')

...将冻结脚本(就像在等待某事一样)。

有什么建议吗?

I am writing a python script (Linux) that is adding some shell aliases (writes them to HOME/.bash_aliases).

In order to make an alias available immediately after it was written I should issue the following bash built-in:

source HOME/.bashrc

source is a bash built-in so I cannot just:

os.system(source HOME/.bashrc)

If i try something like:

os.system('/bin/bash -c source HOME/.bashrc')

...will freeze the script (just like is waiting for something).

Any suggestions ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吻泪 2024-09-25 01:46:30

你想做的事是不可能的。或者更好:你试图如何做到这一点是不可能的。

  1. 你的 bash 命令是错误的。 bash -s command 不执行命令。它只是将字符串“command”存储在变量 $1 中,然后将您带到提示符处。这就是 python 脚本似乎冻结的原因。你想要做的是bash -c命令

  2. 为什么要源.bashrc?仅获取 .bash_aliases 还不够吗?

  3. 即使您的 bash 命令正确,更改也只会在从 python 启动的 bash 会话中生效。一旦 bash 会话关闭,并且 Python 脚本完成,您就会回到原来的 bash 会话。从 python 启动的 bash 会话中的所有更改都会丢失。

每次你想在当前 bash 会话中更改某些内容时,你都必须在当前 bash 会话中进行操作。您从 bash 运行的大多数命令(系统命令、Python 脚本,甚至 bash 脚本)都会生成另一个进程,并且您在该其他进程中执行的所有操作都不会影响您的第一个 bash 会话。

source 是一个 bash 内置命令,它允许您在当前运行的 bash 会话中执行命令,而不是生成另一个进程并在那里运行命令。定义 bash 函数是在当前运行的 bash 会话中执行命令的另一种方法。

请参阅此答案了解有关采购和执行的更多信息。

您可以做什么来实现您想要的目标

修改您的Python脚本以仅对.bash_aliases进行必要的更改。

准备一个 bash 脚本来运行 python 脚本,然后源 .bash_aliases

#i am a bash script, but you have to source me, do not execute me.
modify_bash_aliases.py "$@"
source ~/.bash_aliases

向您的 .bashrc 添加别名以获取该脚本,

alias add_alias='source modify_bash_aliases.sh'

现在当您在 bash 提示符中键入 add_alias some_alias 时,它将被 sourcemodify_bash_aliases.sh 替换> 然后执行。由于 source 是 bash 内置命令,因此脚本中的命令将在当前运行的 bash 会话中执行。 python 脚本仍将在另一个进程中运行,但后续的 source 命令将在当前运行的 bash 会话中运行。

另一种方法

是修改 python 脚本,只对 .bash_aliases 进行必要的更改。

准备一个 bash 函数来运行 python 脚本,然后源 .bash_aliases

add_alias() {
  modify_bash_aliases.py "$@"
  source ~/.bash_aliases      
}

现在您可以像这样调用该函数:add_alias some_alias

what you are trying to do is impossible. or better: how you are trying to do it is impossible.

  1. your bash command is wrong. bash -s command does not execute command. it just stores the string "command" in the variable $1 and then drops you to the prompt. that is why the python script seems to freeze. what you meant to do is bash -c command.

  2. why do you source .bashrc? would it not be enough to just source .bash_aliases?

  3. even if you got your bash command right, the changes will only take effect in the bash session started from python. once that bash session is closed, and your python script is done, you are back at your original bash session. all changes in the bash session started from python is lost.

everytime you want to change something in the current bash session, you have to do it from inside the current bash session. most of the commands you run from bash (system commands, python scripts, even bash scripts) will spawn another process, and everything you do in that other process will not affect your first bash session.

source is a bash builtin which allows you to execute commands inside the currently running bash session, instead of spawning another process and running the commands there. defining a bash function is another way to execute commands inside the currently running bash session.

see this answer for more information about sourcing and executing.

what you can do to achieve what you want

modify your python script to just do the changes necessary to .bash_aliases.

prepare a bash script to run your python script and then source .bash_aliases.

#i am a bash script, but you have to source me, do not execute me.
modify_bash_aliases.py "$@"
source ~/.bash_aliases

add an alias to your .bashrc to source that script

alias add_alias='source modify_bash_aliases.sh'

now when you type add_alias some_alias in your bash prompt it will be replaced with source modify_bash_aliases.sh and then executed. since source is a bash builtin, the commands inside the script will be executed inside the currently running bash session. the python script will still run in another process, but the subsequent source command will run inside your currently running bash session.

another way

modify your python script to just do the changes necessary to .bash_aliases.

prepare a bash function to run your python script and then source .bash_aliases.

add_alias() {
  modify_bash_aliases.py "$@"
  source ~/.bash_aliases      
}

now you can call the function like this: add_alias some_alias

过潦 2024-09-25 01:46:30

我遇到了一个有趣的问题,我需要获取 RC 文件才能在 python 脚本中获得正确的输出。

我最终在我的函数中使用了它,从我需要获取的 bash 文件中引入相同的变量。确保导入了操作系统。

with open('overcloudrc') as data:
    lines = data.readlines()

for line in lines:
    var = line.split(' ')[1].split('=')[0].strip()
    val = line.split(' ')[1].split('=')[1].strip()
    os.environ[var] = val

I had an interesting issue where I needed to source an RC file to get the correct output in my python script.

I eventually used this inside my function to bring over the same variables from the bash file I needed to source. Be sure to have os imported.

with open('overcloudrc') as data:
    lines = data.readlines()

for line in lines:
    var = line.split(' ')[1].split('=')[0].strip()
    val = line.split(' ')[1].split('=')[1].strip()
    os.environ[var] = val
久随 2024-09-25 01:46:30

来自 的工作解决方案我可以使用从 python 脚本执行程序的别名

import subprocess
sp = subprocess.Popen(["/bin/bash", "-i", "-c", "nuke -x scriptpath"])
sp.communicate()

Working solution from Can I use an alias to execute a program from a python script :

import subprocess
sp = subprocess.Popen(["/bin/bash", "-i", "-c", "nuke -x scriptpath"])
sp.communicate()
夏の忆 2024-09-25 01:46:29

你想要的东西是不可能的。程序(您的脚本)无法修改调用者(您运行它的 shell)的环境。

另一种允许您做一些接近的事情的方法是用 bash 函数编写它,该函数在同一进程中运行并且可以修改调用者。请注意,运行时期间的采购可能会产生负面影响,具体取决于用户 bashrc 中的内容。

What you want is not possible. A program (your script) cannot modify the environment of the caller (the shell you run it from).

Another approach which would allow you to do something close is to write it in terms of a bash function, which is run in the same process and can modify the caller. Note that sourcing during runtime can have possible negative side-effects depending on what the user has in their bashrc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文