Share data between bash, python, and C

Question:

I am working on a project that has a bunch of c and python files. I am using a menu function using a bash script on a Linux terminal. The bash terminal has some menu options and also puts the files in a loop. I.e. the files are executed cyclically. So, I am doing something like the following in the bash script

 #!/bin/bash

PS3='Please enter your choice: '
options=("Loop" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Loop")
            echo "Looping the programs"
            while [ <some test> ]
            do
                  ./cprogram1 arg1 arg2 arg3 ... #calling the c program
                   python3 pythonprogram1.py     #calling the python program
                   sleep .5 # Waits 0.5 second.
            done
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

say cprogram1 spits out some data cprog1data. How can that data be accessed by the pythonprogram1.py?

One way I thought of was to have a log.txt file from which the C program can write and the python program can read. Is this correct? Are there better and faster ways to do this?

Answers:

I think your best option is to use a pipe tp send data from the cprogram1 directly into pythonprogram1 as it is generated. If cprogram1 is designed to finish running before pythonprogram1 even begins, then you could store the output of cprogram1 in a variable in your bash script and use it as an argument for pythonprogram1.

Depending on how cprogram1 and pythonprogram1 run, having them share a file such as log.txt could introduce issues when one program is has the file open and the other is trying to access it.

Answered By: Ethan Thompson
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.