简体   繁体   English

从bash脚本访问python字典

[英]accessing python dictionary from bash script

I am invoking the bash script from python script. 我正在从python脚本调用bash脚本。

I want the bash script to add an element to dictionary "d" in the python script 我希望bash脚本在python脚本中向字典“ d”添加元素

abc3.sh : abc3.sh

#!/bin/bash
rank=1
echo "plugin"
function reg()
{
    if [ "$1" == "what" ]; then
        python -c 'from framework import data;data(rank)'
        echo "iamin"
    else
        plugin
    fi
}

plugin()
{
    echo "i am plugin one"
}

reg $1

python file: python文件:

 import sys,os,subprocess
    from collections import *
    subprocess.call(["./abc3.sh what"],shell=True,executable='/bin/bash')

    def data(rank,check):
       d[rank]["CHECK"]=check
    print d[1]["CHECK"]

If I understand correctly, you have a python script that runs a shell script, that in turn runs a new python script. 如果我理解正确,则您有一个运行shell脚本的python脚本,而该脚本又运行了一个新的python脚本。 And you'd want the second Python script to update a dictionnary in the first script. 并且您希望第二个Python脚本更新第一个脚本中的字典。 That will not work like that. 那将不会那样工作。

When you run your first python script, it will create a new python process, which will interpret each instruction from your source script. 当您运行第一个python脚本时,它将创建一个新的python进程,该进程将解释源脚本中的每条指令。

When it reaches the instruction subprocess.call(["./abc3.sh what"],shell=True,executable='/bin/bash') , it will spawn a new shell (bash) process which will in turn interpret your shell script. 当到达指令subprocess.call(["./abc3.sh what"],shell=True,executable='/bin/bash') ,它将产生一个新的shell(bash)进程,该进程将依次解释您的外壳脚本。

When the shell script reaches python -c <commands> , it invokes a new python process. 当shell脚本到达python -c <commands> ,它将调用一个新的python进程。 This process is independant from the initial python process (even if you run the same script file). 此过程与初始python过程无关(即使您运行相同的脚本文件)。

Because each of theses scripts will run in a different process, they don't have access to each other data (the OS makes sure that each process is independant from each other, excepted for specific inter-process communications methods). 由于这些脚本中的每个脚本都将在不同的进程中运行,因此它们无法访问彼此的数据(操作系统确保每个进程彼此独立,但特定的进程间通信方法除外)。

What you need to do: use some kind of interprocess mechanism, so that the initial python script gets data from the shell script. 您需要做的是:使用某种进程间机制,以便初始python脚本从shell脚本获取数据。 You may for example read data from the shell standard output, using https://docs.python.org/3/library/subprocess.html#subprocess.check_output 例如,您可以使用https://docs.python.org/3/library/subprocess.html#subprocess.check_output从Shell标准输出中读取数据。

Let's suppose that you have a shell plugin that echoes the value: 假设您有一个回显该值的shell插件:

echo $1 12

The mockup python script looks like (I'm on windows/MSYS2 BTW, hence the strange paths for a Linux user): 样机python脚本看起来像(我在Windows / MSYS2 BTW上,因此对于Linux用户来说是奇怪的路径):

import subprocess

p = subprocess.Popen(args=[r'C:\msys64\usr\bin\sh.exe',"-c","C:/users/jotd/myplugin.sh myarg"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
o,e= p.communicate()
p.wait()
if len(e):
    print("Warning: error found: "+e.decode())

result = o.strip()
d=dict()
d["TEST"] = result
print(d)

it prints the dictionary, proving that argument has been passed to the shell, and went back processed. 它打印字典,证明参数已传递给外壳,并返回处理。 Note that stderr has been filtered out to avoid been mixed up with the results, but is printed to the console if occurs. 请注意,stderr已被过滤掉以避免与结果混淆,但是如果发生,它将被打印到控制台。

{'TEST': b'myarg 12'}

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

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