0

I have a bash script (a very basic one) that is called with system() from a c program.

This scirpt calls another script called ./dslite.sh which flashes firmware to a device.

How could I return the value returned by ./dslite.sh to the c program?

C program:

system("flash_firmware.sh");

Script

#!/bin/bash

./dslite.sh --mode flash --config=~/configs/device1_config/c1dut2.ccxml ~/images/$1
too honest for this site
  • 11,417
  • 3
  • 27
  • 49
artic sol
  • 301
  • 4
  • 17
  • 3
    Possible duplicate of [C: Run a System Command and Get Output?](https://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output) – TheNavigat Jul 27 '17 at 11:41

2 Answers2

1

From the Bash manual page, the section about EXIT STATUS:

Bash itself returns the exit status of the last command executed, unless a syntax error occurs, in which case it exits with a non-zero value.

That means the exit code of your script will be the exit code of the ./dslite.sh script.

In other words, you should not have to do anything, the return value of the system function should be what ./dslite.sh returned.


If on the other hand you mean the output that the script prints then use popen instead.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
0

The prototype of system() is:

int system(const char *command);

It does return an int.

The following excerpt is from system()'s manual page:

If all system calls succeed, then the return value is the termination status of the child shell used to execute command. (The termination status of a shell is the termination status of the last command it executes.)

So, as long as there is no error at the moment creating the shell or the shell's corresponding child process, system() returns the value of the last command the script executes.

火乔治
  • 19,950
  • 3
  • 44
  • 60