$? and main return value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting $? and main return value
# 1  
Old 10-12-2010
$? and main return value

It seems $? can only contain one byte. see below:

Code:
int main()
{
       return 0x12345678;
}

After I run the code and check the $? value it output 120, obviously it is the decimal of the last byte of 0x12345678. And if I return a value less than 255, the $? value seems right.

Why? and if $? can only contain one byte how to get the real return value of main() ? thx.
# 2  
Old 10-12-2010
While main() may return an int, I believe the valid range is 0-255 as far as most shells are concerned. I know ksh93 uses only the least significant 8 bits.
# 3  
Old 10-12-2010
Quote:
Originally Posted by vistastar
It seems $? can only contain one byte. see below:

Code:
int main()
{
       return 0x12345678;
}

After I run the code and check the $? value it output 120, obviously it is the decimal of the last byte of 0x12345678. And if I return a value less than 255, the $? value seems right.

Why? and if $? can only contain one byte how to get the real return value of main() ? thx.
The shell will only keep an unsigned byte in the status return value (values 0...255). It takes the exit value and just looks at that last byte.
The exit/return code is to be able to signal success or failure of a program not for regular output.

To get the output of your program you should write the output to stdout and capture it with something like:
Code:
output=$(/path/Your_Program)

or:
Code:
/path/Your_Program | while read line
do
  ...
done

# 4  
Old 10-13-2010
Thank you very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

2. UNIX and Linux Applications

Sendmail: help with main.mc

We compile the sendmail.cf file using main.mc. I have managed to place all of our configuration options in main.mc without a problem, with the exception of one. I want to have the HelpFile commented out by default in sendmail.cf. How can I instruct main.mc to place an entry for HelpFile in... (1 Reply)
Discussion started by: dangral
1 Replies

3. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

4. Programming

main function

Is it possible to execute any function before main() function in C or C++. (6 Replies)
Discussion started by: arun.viswanath
6 Replies

5. UNIX for Dummies Questions & Answers

main makefile

hello everyone.. am having problem with my so-called main makefile all my files are in the same folder, i was planning to make a single makefile that would make two executable but I got some errors so I separated them and used now i was wondering if I could create a single makefile... (6 Replies)
Discussion started by: ideur
6 Replies

6. UNIX for Dummies Questions & Answers

Main UNIX file?

I am restoring data off of a backup tape onto a server I have. The data is coming from an old server I used to have, different IP and different Name. I want to transfer ALL the data from the tape, but yet not have any of my unix network configurations or name settings (on the server itself) to... (3 Replies)
Discussion started by: HandleX
3 Replies

7. Programming

Main Resursive is it possible

Friends I am Trying this code to call main Recursively But It is showing segmentation errot #include<stdio.h> int main() { int i; for(i=0;i<10;i++) { if(i==5) { exit(0); } else { main(); } } } (7 Replies)
Discussion started by: krishna_sicsr
7 Replies

8. Programming

Return value (int) from main to calling shell

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... (3 Replies)
Discussion started by: flounder
3 Replies

9. HP-UX

How to go to main prompt

Hi: I am new to HP-UX. After the system starts up, it is written in the documents that press any key within 10 sec to interrupt, and go to the main prompt, where I can select the boot device or boot from CD to recover system. However if I interrupt boot, it goes straight to HPUX> prompt. Please... (2 Replies)
Discussion started by: amitra123
2 Replies
Login or Register to Ask a Question