The Computer Oracle

Run python oneliner in bash script

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Droplet of life

--

Chapters
00:00 Run Python Oneliner In Bash Script
00:54 Accepted Answer Score 7
01:38 Thank you

--

Full question
https://superuser.com/questions/372603/r...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash

#avk47



ACCEPTED ANSWER

Score 7


There is no need for escaping or moving the argument to its own variable.

But, keeping it mostly the same, the following works for me:

#!/usr/bin/env bash
WS="/Users/danielbeck/Desktop"
PY_GET_MVN_VERS="from xml.dom.minidom import parse;dom = parse('${WS}/pom.xml');print [n.firstChild.data for n in dom.childNodes[0].childNodes if n.firstChild and n.tagName == 'version']"
function test_mvn {
        MVN_VER=$( python -c "${PY_GET_MVN_VERS}" )
        echo ${MVN_VER}
}
test_mvn

/Users/danielbeck/Desktop/pom.xml is the example minimal POM from the Maven docs:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1</version>
</project>

Output:

[u'1']

Please throw away your code and just use mine (after adjusting WS) instead of adjusting yours until it works. You have quite a few syntax errors in there.