My intent is to use 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 again in C. I want to use one common library for functions I need. Here is my C code and functions.sh.
#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))
{
printf("killed by signal: %d\n", WTERMSIG(status));
exit(1);
}
else if (WIFSTOPPED(status))
{
printf("stopped by signal: %d\n", WSTOPSIG(status));
exit(1);
}
else if (WIFCONTINUED(status))
{
printf("continued, unexpected..bailing\n");
exit(1);
}
}
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);
}
}
Please note that here 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.
#!/bin/sh
# functions.sh
myfunc()
{
echo "I received $1"
}
Question: 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 infunctions.shas I have done? - 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?
functions.sh, or remove that from your post (as it is extraneous if it doesn't have the actual code)? – Kazark Feb 2 at 1:32