C shell call script call

I have a small C program calling the shell script myScript.sh. I get the ret value as 256. Please help me find out what went wrong with the system call?

int main()
{
int ret;
ret = system (myScript.sh);
ret >>=  ret;
if (ret != 0)
{
  printf("ret is [%d]",ret);
}
}

Working with a 64-bit UNIX operating system and using the ksh shell

+3
source share
8 answers

system * nix, fork, exec /bin/sh -c, , system , /bin/sh, . wait, /bin/sh, , script, system .

wait:

main 3 wait

, , , .

WIFEXITED(stat_val) , , . exit. , WEXITSTATUS(stat_val), , .

WIFSIGNALED(stat_val) , , , WTERMSIG(stat_val) , .

, , , , , , .

, . fork , system -1 errno, . fork , . , system , , errno, , , , .

perror, , errno.

fork , , , script. echo script C.

access, , / .

Linux, :

strace -o my_program.strace -f ./my_program

ltrace -o my_program.ltrace -f -S ./my_program

( -o), , ​​ . ltrace , , strace , -S ltrace . -f , .

, , ksh

, system Posix /bin/sh . , /bin/sh /bin/ksh script ( ​​ #! script), ), . shell-, , . :

. myshell.sh

, ( ). :

int x = system(". myshell.sh");

.

+4

man system :

 The system() function returns the exit status of the shell as returned by
 waitpid(2), or -1 if an error occurred when invoking fork(2) or
 waitpid(2).  A return value of 127 means the execution of the shell
 failed.

waitpid , WEXITSTATUS(), .

, ret >>= ret, .

+5

:

  • .
  • , ( ).

0x0100 256 , script 1. script , 0, .

+2

( ):

6.5.7/3 [ → ] , undefined.

,

ret >>= ret;

ret < 0 ret >= CHAR_BIT * sizeof (int)...


system -1 .

, ret >>= ret; ( , ret = -1 >> -1;) -, : .

- , C -... - ( , , , , , ,...,...,...)

+2

, script , .

+1

. ? :


        -1 (, fork (2) ), . , (2). , WEXITSTATUS (). , /bin/sh , , (127).

256 -1, .

0

? ret → = ret, .

0

linux, script ( "sh script.sh" )

0

Source: https://habr.com/ru/post/1775014/


All Articles