简体   繁体   English

python 3:如何获取 bash 脚本并在同一个 shell 中运行它

[英]python 3: How to source a bash script and run it in same shell

i am trying to source a shell script having functions.我正在尝试获取具有功能的 shell 脚本。 than trying to execute it like below.而不是像下面那样尝试执行它。

source ~/abc.sh; abc arg1 arg2 arg3 arg4a

It works in unix shell.它在 unix shell 中工作。 But when I am trying to execute same from inside python script it is giving error但是当我试图从 python 脚本内部执行相同的操作时,它会给出错误

    def subprocess_cmd(command):
        process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
        proc_stdout = process.communicate()[0].strip()
        return proc_stdout
    command = "bash -c source ~/abc.sh; abc arg1 arg2 arg3 arg4a"
    out = subprocess_cmd(command)
    print(out)

when i am executing above python code, it is giving below error.当我在 python 代码上方执行时,它给出了以下错误。

~/abc.sh: line 0: source: filename argument required
source: usage: source filename [arguments]
/bin/sh: line 1: abc: command not found

From Popen reference:来自Popen参考:

On POSIX with shell=True, the shell defaults to /bin/sh.在 shell=True 的 POSIX 上,shell 默认为 /bin/sh。 If args is a string, the string specifies the command to execute through the shell.如果 args 是字符串,则该字符串指定要通过 shell 执行的命令。 This means that the string must be formatted exactly as it would be when typed at the shell prompt.这意味着字符串的格式必须与在 shell 提示符下键入时的格式完全相同。 This includes, for example, quoting or backslash escaping filenames with spaces in them.这包括,例如,引用或反斜杠转义文件名,其中包含空格。

So what you're passing has to be passed as a single shell command.因此,您传递的内容必须作为单个 shell 命令传递。

When I run your single POSIX shell command in my shell:当我在我的 shell 中运行你的单个 POSIX shell 命令时:

$ bash -c source ~/abc.sh; abc arg1 arg2 arg3 arg4a
~/abc.sh: line 0: source: filename argument required
source: usage: source filename [arguments]
bash: abc: command not found

So there's nothing special about python here.所以这里的python没有什么特别之处。

You get this error because this command amounts to:您会收到此错误,因为此命令相当于:

  • Original POSIX shell creates a new bash shell process原始 POSIX shell 创建一个新的 bash shell 进程
    • New bash shell sources abc.sh新的 bash shell 源abc.sh
    • Command abc now available in the new bash shell命令abc现在在新的 bash shell 中可用
    • New bash shell terminates新的 bash shell 终止
  • Original POSIX shell tries to use command abc原始 POSIX shell 尝试使用命令abc
  • Original POSIX shell terminates原始 POSIX shell 终止

What you want to do is:你想做的是:

  • Original POSIX shell creates a new bash shell process原始 POSIX shell 创建一个新的 bash shell 进程
    • New bash shell sources abc.sh新的 bash shell 源abc.sh
    • Command abc now available in the new bash shell命令abc现在在新的 bash shell 中可用
    • New bash shell tries to use command abc新的 bash shell 尝试使用命令abc
    • New bash shell terminates新的 bash shell 终止
  • Original POSIX shell terminates原始 POSIX shell 终止

So you want the following 2 commands in the same shell:因此,您希望在同一个 shell 中使用以下 2 个命令:

source ~/abc.sh
abc arg1 arg2 arg3 arg4a

Namely:即:

bash -c 'source ~/abc.sh; abc arg1 arg2 arg3 arg4a'

(note where the single quotes are.) (注意单引号在哪里。)

In python:在蟒蛇中:

def subprocess_cmd(command):
    process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
    proc_stdout = process.communicate()[0].strip()
    return proc_stdout
command = "bash -c 'source ~/abc.sh; abc arg1 arg2 arg3 arg4a'"
out = subprocess_cmd(command)
print(out)

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

相关问题 如何使用 python 将 shell 脚本作为源运行以设置环境变量? - How to run a shell script as source using python to set environment variables? 如何在同一个 IDLE shell 中多次运行 python 脚本,而不关闭和重新打开 shell? - How can I run a python script more than once in the same IDLE shell, without the shell closing and reopening? 如何在linux可执行shell中运行python脚本 - How to run python script in linux Executable shell Python 脚本直接通过命令行运行,但不通过 shell/bash 脚本运行 - Python script runs directly via command line but does not run via shell/bash script 如何将python变量传递给同一文件的bash shell部分? - How to pass python variable to bash shell part of the same file? Python脚本运行Bash命令 - Python Script To Run Bash command 如何使用相同的端口运行 python 脚本 - How to run python script with the same port 如何从同一个 python 脚本运行 2 个爬虫 - how to run 2 crawlers from the same python script 如何使用Python脚本在Shell / Linux中运行sudo命令 - How to run sudo commands in shell/linux using python script 如何在激活 python virtualenv 后自动运行 shell 脚本? - How to automatically run a shell script after activation of python virtualenv?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM