user1089679 user1089679 - 3 months ago 10
Linux Question

How can I check for successful execution of last call of system()

Here I want to know the status of the last executed call of

system()
.

I have one script file containing

hciconfig hci0 &> /dev/null
if [ "$?" -ne 0 ]; then
. ./$BT_CLEAN


I want to do same thing in a C Program so I used
system()
to run the command
hciconfig hci0 &>
/dev/null
. But how can I know the status of this executed command? In the shell script we used "$?" same here In C: How can I know whether the last command was executed successfully or not?

I used
system("hciconfig hci0 &> /dev/null")
in C.

NPE NPE
Answer

To quote man 3 system:

system() returns -1 on error (e.g. fork(2) failed), and the return status of the command otherwise.