What is the sytax to return an int from C program main back to calling shell?
#!/usr/bin/ksh
typeset -i NO_RECS
$NO_RECS=process_file
# Process file is a C program that is set up to return an int from main. The
#program complies with no issues, but an error is generated when the shell
#calls the program. Is the syntax correct to return the int value to a shell
#variable?
Here is the C program:
int main(argc,argv)
{
int no_recs_tot ;
/* This is just a function within the c program that returns the int value */
no_recs_tot = bld_detail(v_out_path,v_in_path,inrec_cnt,v_src_data_dt);
return (no_recs_tot);
} /* End main */
Code tags please. Like {code} int main(); {/code} but with [ ] instead of { }.
The syntax for returning a code to the shell is exactly as you show it. What might be wrong is the VALUE you return. Any non-zero value means some sort of error. Zero means success.
Plus, on a POSIX compliant system :
int main()
{
return 42001;
}
$ cc -o testc test.c
$ testc
$echo $?
17
because 42001 % 512 = 17. There is a max value allowed for return codes. So, if the OP has several thousand records the value will be meaningless.
Return codes are for program status.
First of all,
#!/usr/bin/ksh
typeset -i NO_RECS
$NO_RECS=process_file
Will not print the return code. The syntax is wrong to begin with, it should be
NO_RECS=$(process_file)
The second thing is that the return code of any command executed in the shell is not printed, but stored in a variable $?. To directly get the value that your C program has, you should do it like this:
#!/usr/bin/ksh
typeset -i NO_RECS
NO_RECS=$(process_file)
And your C program should be:
int main(argc,argv)
int argc; char *argv[];
{
int no_recs_tot ;
/* This is just a function within the c program that returns the int value */
no_recs_tot = bld_detail(v_out_path,v_in_path,inrec_cnt,v_src_data_dt);
fprintf(stdout,"%d",no_rec_tot);
} /* End main */