简体   繁体   English

是否可以通过非登录shell的单行命令从bash登录shell执行python脚本?

[英]Is it possible to execute a python script from a bash login shell with a one-line command from a non-login shell?

I have a use case where I need to execute a Python script I'm working on from a login shell due to a dependency on values sourced from /etc/profile.d/. 我有一个用例,由于依赖于/etc/profile.d/中的值,因此需要从登录shell执行我正在处理的Python脚本。 There's several combinations I've tried, but I seem to run into one issue or another. 我尝试了几种组合,但似乎遇到了一个问题或另一个问题。

This is nested in another automated process that's running in a non-login shell so I'm looking for a one-line command to get this to work. 这嵌套在另一个在非登录外壳程序中运行的自动化过程中,因此我正在寻找一个单行命令来使其工作。

Does anyone know of a one-line command to get this to work? 有谁知道单行命令使它起作用?

For example, I tried the following, but it failed with the "cannot execute binary file" error: 例如,我尝试了以下操作,但由于“无法执行二进制文件”错误而失败:

bash --login python my_python_script.py
/usr/bin/python: /usr/bin/python: cannot execute binary file

I've also tried a few other combinations with and without the shebang. 我也尝试了带有或不带有shebang的其他几种组合。 Any help is appreciated. 任何帮助表示赞赏。

You need -c there before the command and then you need to quote the command as a single argument. 在命令前需要-c ,然后需要将命令作为单个参数引用。

bash --login -c 'python my_python_script.py'

If the shell also needs to be an interactive shell then the -i option needs to be used there also. 如果外壳也需要是交互式外壳,则还需要在其中使用-i选项。

bash --login -i -c 'python my_python_script.py'

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

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