从bash-python-bash传递变量

时间:2021-08-11 23:20:22

To use logarithmic function, I used export to pass a variable $var1 from bash to python script. After the calculation, I used

为了使用对数函数,我使用export将变量$ var1从bash传递给python脚本。经过计算,我用了

  os.environ['var1']=str(result)

to send the result back to bash script. However, the bash still shows the unmodified value.

将结果发送回bash脚本。但是,bash仍显示未修改的值。

1 个解决方案

#1


-1  

You can have a look at the os.putenv(key, value) function here maybe it could help you.
Although as noted on the doc :

您可以在这里查看os.putenv(键,值)函数,也许它可以帮助您。虽然正如文件中所述:

When putenv() is supported, assignments to items in os.environ are automatically translated into corresponding calls to putenv(); however, calls to putenv() don’t update os.environ, so it is actually preferable to assign to items of os.environ.

当支持putenv()时,os.environ中项目的赋值会自动转换为对putenv()的相应调用;但是,对putenv()的调用不会更新os.environ,因此实际上最好分配给os.environ的项目。

EDIT :
moooeeeep thought about it just a minute before me, but the variable you change only applies to the current process. In such a case I can see two solutions :
- Write the data to a file and read it with your bash script
- Call your bash script directly from within python so that the bash process would inherit your modified variable.

编辑:moooeeeep在我之前的一分钟考虑过它,但你改变的变量只适用于当前的过程。在这种情况下,我可以看到两种解决方案: - 将数据写入文件并使用bash脚本读取 - 直接从python中调用bash脚本,以便bash进程继承修改后的变量。

#1


-1  

You can have a look at the os.putenv(key, value) function here maybe it could help you.
Although as noted on the doc :

您可以在这里查看os.putenv(键,值)函数,也许它可以帮助您。虽然正如文件中所述:

When putenv() is supported, assignments to items in os.environ are automatically translated into corresponding calls to putenv(); however, calls to putenv() don’t update os.environ, so it is actually preferable to assign to items of os.environ.

当支持putenv()时,os.environ中项目的赋值会自动转换为对putenv()的相应调用;但是,对putenv()的调用不会更新os.environ,因此实际上最好分配给os.environ的项目。

EDIT :
moooeeeep thought about it just a minute before me, but the variable you change only applies to the current process. In such a case I can see two solutions :
- Write the data to a file and read it with your bash script
- Call your bash script directly from within python so that the bash process would inherit your modified variable.

编辑:moooeeeep在我之前的一分钟考虑过它,但你改变的变量只适用于当前的过程。在这种情况下,我可以看到两种解决方案: - 将数据写入文件并使用bash脚本读取 - 直接从python中调用bash脚本,以便bash进程继承修改后的变量。