LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-30-2010, 05:12 AM   #1
madyogi
LQ Newbie
 
Registered: Feb 2009
Location: Poland
Distribution: My own, Fedora, OpenSuse
Posts: 21

Rep: Reputation: 2
Question Catching signed and unsigned integers in BASH


Solving issues with signed and unsigned numbers in BASH.

For a start, Yes, BASH is type independent – I know that. My problem lays in catching executables output into a BASH variable.

My executables are not quite UNIX compatible, where returned values are 0 for OK, >0 ERROR. They return 0 for OK, >0 WARNING (only, so move on) and <0 ERROR (abort) instead.

Code:
// C++ BIN A
int main(){
	// some code...
	return 1;
}

// C++ BIN B
int main(){
	return -1;
}

// C++ BIN C
int main(){
	return 255;
}
Below is the part of a test suite in BASH to verify the output. I compile and execute the A, B and C, binaries in a loop as $mybinary.
Code:
#!/bin/bash

# (…)
# here is a piece of the loop
$mybinary
RETURN=$?
if (( $RETURN >= 0 )); then
  let testOk=$testOk+1
  RMSG=OK
else
 let testFail=$testFail+1
 RMSG=ERROR
fi
echo [ $RETURN ] RESULT: $RMSG
# (...)
The test programs A, B, and C should return 1, -1 and 255 respectively. A is fine, 1 is returned. The problem is that programs B and C both return 255 value. I know -1 is hex 0xff (signed) which is equivalent to 0xff (unsigned) and is back-equal to decimal 255.

The question is, how to determine a real decimal 255 value from a negative -1 in BASH?

Is the '$?' 8-bit aligned (dynamically shortened) when no higher values expected?

Have to mention that, I'm considering a use of “cout << mynumber;” before each last “return mynumber” and grab the output with RETURN=`mybinary`, from the screen. Yes, it works but looks ugly.
 
Old 12-30-2010, 05:49 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
-1 is not a valid exit status (A.K.A return code) according to GNU's exit status documentation which only allows for exit statuses between 0 and 255.

When checking integers in bash, using string comparison operators such as >= does not work (depending on the values it can gve the intended result), hence the numeric comparison operators such as -le
 
Old 12-30-2010, 12:58 PM   #3
madyogi
LQ Newbie
 
Registered: Feb 2009
Location: Poland
Distribution: My own, Fedora, OpenSuse
Posts: 21

Original Poster
Rep: Reputation: 2
Unhappy Good point

Quote:
-1 is not a valid exit status (A.K.A return code) according to GNU's exit status documentation which only allows for exit statuses between 0 and 255.
Well -1 is not compatible for GNU/UNIX standards as a return code. I didn't say I'm writing a fully compatible code. Just making it as much portable as possible. Anyway, this is a good link, you have posted and since this is a Linux/GNU dedicated forum I'm happy reading this.

Well. I saw the information about 0-255 limitation somewhere, in the bash docs (I guess). Can't find it out now. From tests I already made, the shell acts like it doesn't know if it is using a signed or unsigned value. From where should it know about that, I think. But where did the limit of 255 came from?
Did some more tests and aligned the return code on 16 bits (short int) inside the the B & C . When debugging the executable everything goes fine up till the end of main.

When libc takes over control and receives a value greater than 0xff (like 0x102), It "forgets" the hi-byte and informs that program exited with exit code "0x02".
Strange thing to mention is case when 0x100 or anything higher, having lower bits zeroed shows up. The libc claims that "program exited normally" so, without any errors. Gives a bit to think about...
Still wanting to be 8-bit guys friendly? Wow... respect this, but now thinking on how to quickly rewrite pieces of my code...

My executables A,B and C are used as part of a test suite for a library I'm developing. Since it had to be OS independent, I have decided to use 16 or 32 bits as return codes, where values greater than 0 are for warnings only and leaving negative numbers (16th or 32nd bit enabled) for errors...

Guess now my executables will have few extra lines to replace the negative numbers with a value of 2, positive numbers with 1 and leave a 0 happy as it was before...

Thank you for your quick replay.
 
Old 12-30-2010, 01:08 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by madyogi View Post
But where did the limit of 255 came from?
From the linked page, the exit status is the "low-order 8 bits of the exit status value" (the exit status is an int) and, as you already know, the maximum value of an 8-bit unsigned integer is 255. Or have I answered a different question from the one you asked?
 
Old 12-30-2010, 01:50 PM   #5
madyogi
LQ Newbie
 
Registered: Feb 2009
Location: Poland
Distribution: My own, Fedora, OpenSuse
Posts: 21

Original Poster
Rep: Reputation: 2
Talking Smile on my face.

Lol, You're fast!
Quote:
But where did the limit of 255 came from?
Yes, It was only a rhetorical question...
But let's not spam anymore.
Cheers...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
signed and unsigned in C noir911 Programming 10 06-02-2009 08:58 AM
unsigned integers in C CoderMan Programming 5 03-24-2009 07:50 PM
signed/unsigned int and vector::size() [KIA]aze Programming 1 11-23-2007 06:19 PM
signed and unsigned ArthurHuang Programming 4 05-23-2006 03:46 AM
Adding Unsigned Integers of 128 bit bluechicken Programming 1 06-27-2005 02:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration