简体   繁体   English

在执行bash脚本时将变量Bash到Python

[英]Bash variables to Python while executing bash script

I read the related Questions before asking my question but it's kind of problem specific. 在问问题之前,我先阅读了相关的问题,但这是特定于问题的。 I am executing a bash script and the result is stored in a .txt file. 我正在执行bash脚本,结果存储在.txt文件中。 Immediately after the file is created, I need to use a number which is contained in the file to do some operations in Python. 创建文件后,我需要立即使用文件中包含的数字在Python中进行一些操作。 I take the variable of interest using grep as follows. 我使用grep如下关注变量。

MeasureImageSimilarity.sh 3 2 myfile1 myfile2 results.txt
var=$(grep -Ewo "[+-]?[0-9]" result.txt)

My question now is how can i create an array of these "var" variables in Python if i put the above script in a loop. 现在我的问题是,如果将上述脚本放入循环中,如何在Python中创建这些“ var”变量的数组。 I am asking because i also need the indexes of the hypothetical for-loop that i want to iterate in. 我之所以问是因为我还需要迭代一个假设的for循环的索引。

You could do something like this: 您可以执行以下操作:

a=subprocess.check_output('(grep -Ewo "[+-]?[0-9]" result.txt)', shell=True)

Since this will return a string you can perform: 由于这将返回一个字符串,因此您可以执行以下操作:

a=float(a)

Hope it helps! 希望能帮助到你!

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

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