How to resolve this bash python interaction? -


i have 3 files, let's see 1 one.

params.conf

[a] [b] [c] [d] [e] 

parsing.py

from configparser import safeconfigparser  parser = safeconfigparser() parser.read('params.conf')  lst=parser.sections() print lst 

demo.sh

#! /bin/bash value=$(python parsing.py) echo "$value" 

after running demo.sh, should output ['a','b','c','d','e'] , getting that.

but, there several problem when going next level.

  • i want use list elements, @ moment need use sed parse within bash file don't want, rather love access list array in bash can use later.

  • currently printing list, can't that. because have multiple list in python file, , can't afford print each of them , retrieve bash. so, can return variable python bash somehow, searching hack source bash file bash file.

  • eventually want have multiple functions in python file , try access particular functions return value bash. can achieve that? rather calling whole python file, can call specific function python file?

you not need use sed parse output of python script.

in python script before printing output, convert list space separated string (hope not have item in list space in it)

print " ".join(lst) 

this output can looped through in shell. me following code gives below output.

#! /bin/bash lst=$(python parsing.py) in $lst     echo $i done 

params.conf same yours...

output

a b c d e 

hope helps in solving of problem..

also calling specific function in python shell script can achieved below...

python file multiple function

import sys  def foo():     print 'i foo'  def func():     print 'i func'  def bye():     print 'i bye'  if __name__ == "__main__":     if len(sys.argv)==2:         x = getattr(sys.modules[__name__], sys.argv[1])         x() 

and shell script (i using command line args name python function)

#! /bin/bash echo $(python params.py $1) 

output...

# ./demo.sh foo foo # ./demo.sh func func # ./demo.sh bye bye 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -