C program calling shell script -
i have small c program calling shell script myscript.sh. getting value of ret 256. please me in knowing went wrong system call?
int main() { int ret; ret = system (myscript.sh); ret >>= ret; if (ret != 0) { printf("ret [%d]",ret); } }
working on 64 bit unix operating system , using ksh shell
the way system
function works on *nix calls fork
, child calls 1 of exec
functions /bin/sh
-c
, string passed system
in child, turns child process instance of /bin/sh
program runs command. parent calls 1 of wait
functions, waits /bin/sh
exit, same exit status shell script, , system
returns value.
if @ man pages wait
system call(s):
main 3 wait
you should information gets returns , macro functions make sense of it.
the wifexited(stat_val)
macro can used test if program exited opposed signal. normal exits involve calling exit
system call. if function returns non-zero value can use wexitstatus(stat_val)
macro value returned.
the wifsignaled(stat_val)
macro can used test if program terminated signal, , if wtermsig(stat_val)
macro return signal number caused termination.
there other macros can tell if process stopped or continued, rather terminated, don't think overly helpful purpose, may want them.
as far happening in case, can difficult tell. if fork
call fails system
able return -1 , set errno
reflect error. if fork
did not fail error may have happened in child , more difficult locate. may possible on platform system
might tests before forking insure have permission execute appropriate files , set errno
reflect that, maybe not.
you should perror
function print out error messages in case errno
set.
if failure happens after fork
, within child either need shell tell more happening, or shell script to. may including echo
statements in script using print statements in c programs.
you should access
function test if have permission read and/or execute files.
if using linux should able do:
strace -o my_program.strace -f ./my_program
or
ltrace -o my_program.ltrace -f -s ./my_program
and examine trace files (after -o
) @ programs , kernel each other. ltrace
looks @ how program talks library function, while strace looks @ system calls, -s
tells ltrace @ system calls. -f
argument tells them both trace children of program created.
i noticed said using ksh
as mentioned system
under posix system should use /bin/sh
or compatible shell. doesn't mean /bin/sh
won't run /bin/ksh
run script (or kernel won't use #!
line @ beginning of script file this), problem. there ways run shell scripts line not used know shell used. notable is:
. myshell.sh
the period , space tries dump text of file current shell session rather run in process (this useful setting environment). if doing:
int x = system(". myshell.sh");
then problem.
Comments
Post a Comment