1

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
5
  • Wow. Ultimate search before you post fail. Commented Dec 8, 2015 at 21:14
  • @MadPhysicist, but that answer is old and not Python3 compliant Commented Dec 8, 2015 at 21:19
  • @JohnLaRooy this isn't a Python3 question, and $ python on *nix should always run python2. If you'd like to update the answer, do so on the duplicate. Commented Dec 8, 2015 at 21:25
  • 2
    @AdamSmith, Arch linux already defaults to python3 and Ubuntu 16.04 is planning to move away from python2.., PEP0394 says "for the time being..." Commented Dec 8, 2015 at 21:35
  • @JohnLaRooy is that so? Awesome! Python2 has been a long-rotting corpse as far as I'm concerned. Commented Dec 8, 2015 at 23:21

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for response.. But I need to pass the param dynamically . Like python -c "import foo; print(foo.x(${var1}, ${var2}))"
So do that. What is stopping you?
This is not working on our Red Hat Enterprise Linux Server release 5.11, please help me to identify the alternative

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.