简体   繁体   中英

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. Immediately after the file is created, I need to use a number which is contained in the file to do some operations in Python. I take the variable of interest using grep as follows.

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. I am asking because i also need the indexes of the hypothetical for-loop that i want to iterate in.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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