Question

I am writing an application (CLI Based) in C and I want to be able to run a shell script to do system level commands, (its an OSX Specific app). Is there a way to do this? I tried system() but it says its not valid as of c99.

if (response == 'Y' || response == 'y') {
        system("Support/script.sh");
        system("Support/deps.sh");
        printf("Success");
    } else {
        printf("Good Bye!\n\n");
    }
Was it helpful?

Solution

Check for your current working directory.Looks like te Support folder doesnt exist in the pwd. Mac OS X, an objective-C based, should work with the system calls.

Here is my sample program using popen, if at all you require it. (Just a snippet of my code.. not complete)

char unix_script[1000];
memset(unix_script,'\0',sizeof(unix_script));
snprintf(unix_script,
          sizeof(unix_script),
          "ksh /usr/mahesh/sessioN.ksh %s %s %s %s %s",
          userId,
          password,
          database,
          sbcr_id,
          session_id);
char *COMMAND = unix_script,*readLine, *tmp, *commandResult = "";
FILE * fp;
int status;

fp = popen(COMMAND, "w");

if (fp == NULL) {
      perror("Command execution failed");
      exit(1);
}

//printf("Printing the command output....");
while ((fscanf(fp, "%s", &readLine)) != EOF) {
      tmp = (char *) realloc(commandResult, strlen(readLine));
      commandResult = tmp;
      strcpy(commandResult, readLine);
}
printf("\n output =\n %s\n",commandResult);
status = pclose(fp);
//printf ("Command %s exit status code = %d\n", COMMAND, status);
return status;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top