passing variables from python to bash

A forum for general discussion of the Python programming language.

passing variables from python to bash

Postby corvi » Sun Sep 08, 2013 8:26 am

I am completely new to python and its syntax..
I have a bash-script which includes a python part in this way:
-------------------------------------------------------------------
#!/bin/bash
.. some instructions to get p, q, ... (floating numbers obtained through "awk" call to a datafile)
python >>END
... some calculations using previous bash results (p, q, ..)
a = ..
b = ..
...
END
... some analysis using python results(a, b, ..)
-------------------------------------------------------------------
While I am able to pass bash outputs p, q to python simply through $p, $q, ..,
I did not find the way to pass python outputs a, b, .. to the following bash session.
How to do?
corvi
 
Posts: 4
Joined: Sun Sep 08, 2013 7:40 am

Re: passing variables from python to bash

Postby Somelauw » Sun Sep 08, 2013 8:40 pm

Here are a few ways I can think of:

Code: Select all
$ a=$(python<<END
print("hello")
END)
$ echo $a
hello

But that only works for a single variable.

You can make it work with multiple variables by using mapfile:
http://unix.stackexchange.com/questions ... -not-space

The other way is to execute bash from within python
Code: Select all
subprocess.call(["ls", "-l"])


I advise you to write your script as much as possible using only a single language, though.
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 71
Joined: Tue Feb 12, 2013 8:30 pm


Return to General Discussions

Who is online

Users browsing this forum: snippsat and 1 guest