简体   繁体   中英

how to return the return value of a script to a c program using system()

I have a bash script (a very basic one) that is called with system() from ac 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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM