I am looking for feedback on the design of my program.
I have a shell script call function.sh that defines a lot of helper functions. My intent is to use those bash functions defined in functions.sh in a C program. I am doing this so that I don't have to rewrite the bash functionality (in functions.sh) again in C. I want to use one common library for functions I need (which is functions.sh).
Here is my C code and functions.sh
C code:
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>
void shellCall(char * command)
{
int status, commandexitcode=0;
if(command == NULL)
{
printf("NULL command string sent\n");
exit(1);
}
status = system(command);
if(status == -1)
{
printf("Error during call, value %d\n", status);
exit(1);
}
if (WIFEXITED(status))
{
commandexitcode = WEXITSTATUS(status);
if (commandexitcode != 0)
{
printf("non zero exit code: %d\n", commandexitcode);
exit(1);
}
}
else
if (WIFSIGNALED(status))
...
}
int main(int argc,char **argv)
{
char command[100]; //command
int i = 0;
for (i = 1 ; i <= 10 ; i++)
{
sprintf( command, "source $PWD/functions.sh ; i=%d ; myfunc %d", i,i );
shellCall(command);
}
}
functions.sh:
#!/bin/sh
# functions.sh
myfunc()
{
echo "I received $1"
}
Please note that I have only shown a very simple example of the way I am using functions in functions.sh. In reality functions.sh has many functions, some of which implement decent logic.
My Questions - I need feedback on the following aspects.
Is it a bad programming practice ( something that will make experienced and senior developers mad when they see this) to use system() calls to call functions in functions.sh as I have done?
If it is a bad practice, what are the reasons (technical and other) for which it is considered a bad programming practice?
What changes should I make to my C code so that experienced and senior developers will find it acceptable?