Tuesday, 3 September 2013

Return to main C program after calling shell script

Return to main C program after calling shell script

I have a C program running on my Raspberry-Pi that continuously reads
serial info from an AVR. I want to write the history of commands received
using a simple shell script:
#!/bin/bash
echo "Command $2 received from address $1"
date_time=$(date) #read current date from terminal
echo " $date_time Address: $1 Cmd: $2" >> file.txt #echo address &
command to file, redirect stdio
exit 0
This script works perfectly when called within terminal with the proper
arguments, however, I want to execute it each time that a command is read
from the serial line. The C program currently reads the command & address
and prints it to the standard io, so what I would like to do is call the
script after this, to write data to a file.
When calling the script in my code like this:
char* args[3];
char* program="ioExport.sh";
args[1]=addr;
args[2]=cmd;
args[3]=NULL;
execv(program,args);
it exits the main program. How can I return to the main program after
calling the script? Or is there a way to do it when running the script as
a background process? I'm aware that it would be much simpler to do it
with standard C file i/o, but if it's possible using the shell, I'd like
to do that :)
Regards, Jaco

No comments:

Post a Comment