Can some one help me with the syntax, how to call 'x' function from a shell script. The challenge here is, from shell script need to pass a,b dynamically.
your suggestion greatly appreciated.
Python Code:
def x(a,b):
'''x(a,b) --> [a,b]'''
return [a,b]
def add(a,b):
'''add(a,b) --> a+b'''
return a+b
def test():
'''test code for all modules'''
print "testing x..."
print x(1,2)
print x('a','b')
print x([1,2,3],[4,5,6])
print "testing add..."
print add(1,2)
print add('a','b')
print add([1,2,3],[4,5,6])
return
Mad Physicist
116k29 gold badges202 silver badges292 bronze badges
1 Answer 1
If you save the file foo.py, you can run this from the shell
python -c "import foo; print(foo.x(1, 2))"
The result can be read from stdout.
answered Dec 8, 2015 at 21:14
John La Rooy
306k54 gold badges379 silver badges514 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
SVN
thanks for response.. But I need to pass the param dynamically . Like python -c "import foo; print(foo.x(${var1}, ${var2}))"
Mad Physicist
So do that. What is stopping you?
SVN
This is not working on our Red Hat Enterprise Linux Server release 5.11, please help me to identify the alternative
default
$ pythonon *nix should always run python2. If you'd like to update the answer, do so on the duplicate.