Data exchange between python script and bash script
venkatachalam.19 at gmail.com
Tue Apr 4 13:07:25 EDT 2017
On Tuesday, April 4, 2017 at 5:27:42 PM UTC+2, cassiope wrote:
> On Tue, 04 Apr 2017 08:01:42 -0700, venkatachalam.19 wrote:
> > Hello All,
> > I am writing a python code for processing a data obtained from a sensor. The data from sensor is obtained by executing a python script. The data obtained should be further given to another python module where the received data is used for adjusting the location of an object.
> > For achieving this, there is a central bash script, which runs both the python modules parallel. Something like:
> > python a.py &
> > python b.py &
> What is going on that two python scripts are needed? Which one generates the data needed by the bash script?
One python script communicates with an external sensor and receives data.
Another script controls a robot. That is based on ROS modules.
>From the given code, a.py will be the script generating the data. Both the modules are independent of each other and so I have written both of them in different python scripts.
> > I am trying to return the sensor data to the bash .sh file, therefore it can be provided to the other script. This, based on the online tutorials looks like:
> > sensor_data=$(python execute_sensor_process.py) &
> Presumably <sensor_data> is simply getting the exit status code from the python interpreter, not the data, right?
> What are you seeing?
> I am not getting the exit status (I am not setting or printing return status in the python script). I need both the scripts running in parallel. Meaning the sensor will be continuously monitoring the location of the robot and this data will be used in another python script which controls the robot motion.
For now, I am printing the output in the script and as far I understood, this can be assigned to a variable in the bash file from where this file is executed.
I am not getting any output in the assigned variable in bash.
> > and the sensor_data is assigned by printing the required data in the corresponding python script. For example, the data is printed in execute_sensor_process.py as follows:
> > print >>sys.stderr,sens_data
> > By printing the data onto sys.stderr and assigning a return variable in the bash, I am expecting the data to be assigned.
> Assigned to what? Some return variable in bash? What??
> Why not use stdout? Either pipe the data from python directly into a (possibly modified) bash script,
> or into a file which gets read by the bash script.
Yes. As shown in "sensor_data=$(python execute_sensor_process.py) &", the variable sensor_data is assigned to hold the return value from the python script.
I was using the stdout. But in some forums for similar problems, it is suggested to print the data to stderr (I really dont know what is the significance).
I am unaware of piping the data from python. I will look into this.
> > But this is not happening. The sensor data is a dictionary and I like to have this data for further analysis. I am not getting the data returned from the python script on to the bash variable.
> Bash doesn't have dictionaries like python. Why is bash needed?
Bash is needed to run the different scripts. I tried using multiprocess and thread from python, but really they are not performing parallel processing, when there are completely two different process. I understood it by executing the multiprocess/thread. Sequential operation was happening, which lead to the situation that only one python process is always executed.
> > Can someone help me to understand why the code is not working? I tried other approaches of function call such as
> You haven't given us enough of the code to really answer.
> > sensor_data=$`python execute_sensor_process.py` &
> > python execute_sensor_process.py tempfile.txt &
> > kinexon_data=`cat tempfile.txt` &
> > But none of the approaches are working.
> > Thank you,
> > Venkatachalam Srinivasan
> I wonder if you could completely eliminate the bash script - do it all in python.
> I've written quite a few bash scripts, but not so many since I started using
> python. Only exception is for low level functions on systems without a functioning
More information about the Python-list