-3

i'm trying to use a function from a python script and store it to a variable in a shell script from another directory,here's the visual representation of the directories:

    xxx/
        aaa.sh
    yyy/
        sample.py

here's the code of the sample.py

    import ConfigParser

    def getval(section, key):
    config = ConfigParser.RawConfigParser()
    config.read("sample.config")
    return config.get(section, key)

and here is some part of the aaa.sh

    #!/bin/sh

    Path = $(python -c 'import imp; lp = imp.load.source('sample', 'path/to/sample.py'), print lp.getval('section_1', 'key_1')')

    some code.......

    echo $Path

my problem was i don't get any value of the Path,i think i have mistake on how will i call the python script function,i don't know what part is that. can anyone help me. thanks in advance.

i combined the idea of EOL from this question Calling a Python function from a shell script and the idea of Sebastian Rittau from this question How to import a module given the full path?.

Community
  • 1
  • 1
obutsu
  • 367
  • 1
  • 4
  • 12

1 Answers1

1

Try:

Path=$(python -c 'import imp; lp = imp.load.source("sample", "path/to/sample.py"); print lp.getval("section_1", "key_1")')
cdarke
  • 42,728
  • 8
  • 80
  • 84