0

I have a shell script which does some basic functions and calls the c program with specific command line arguments for each operation.

The C program returns integer value indicating success or failure.

Now I have a situation, where the c program needs to couple of values and that needs to be accessed by the parent script. Is there any way this can be achieved? [Apart from storing the value in a temporary file and the script accessing the file to get the value]

2
  • maybe a pipe if the logic permits Commented Aug 23, 2020 at 15:30
  • Consider haring sample input and output, so that SO readers will understand the problem that you are trying to solve Commented Aug 24, 2020 at 4:39

3 Answers 3

1

Let your C program write the values to its stdout, and use the command substitution shell feature to capture those values in the parent script, along the lines of

program_output=$(your_c_program with the arguments it needs)

man bash (and search for command substitution) for details.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the excecl command

int execl(const char *path, const char *arg, ...);

Like so:

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>

void main () {

   return execl ("/bin/pwd", "pwd", NULL);

}

1 Comment

May be my question is bit confusing. Let me give an example. Lets say I have a script test1.sh, from which I am calling an executable exe1. This exe1 (c program) will get some values say val1 and val2. These values should be returned back to the script test1.sh, so that I can continue with other operations on val1 and val2.
0

yes there is, take a look at the exec system call, there are multiple functions that can help you achieve this, but basically you can execute a c program and pass it all arguments you want/need

https://linuxhint.com/exec_linux_system_call_c/

2 Comments

Basically the shell script is the parent which is calling C program. not the other way round.
let's say your executable is called main and you want to pass the following arguments to it: arg1, arg2 and arg3. you can do this by typing in the shell: ./main arg1 arg2 arg3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.