LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-19-2005, 06:25 PM   #1
anekkan
LQ Newbie
 
Registered: Sep 2003
Posts: 5

Rep: Reputation: 0
running an executable from a bourne shell which returns a value


Hey guys,

I have an executable which returns an integer value.

How do I get that value back in the shell script?
Ex:

C program:

#include...

main()
{
return 500;
}

Shell script:

#!/bin/sh

val=`./prog`
echo "$val"

****
I basically want the value 500 in the shell variable val.

Any thoughts on how this can be done.?
Thanks.
 
Old 09-19-2005, 06:29 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Any thoughts on how this can be done.?
Yes, instead or returning 500, which is pointless as exit values are stored on a byte, and should be zero if the program succeeds, just print the value you want to affect to the variable.
Code:
printf("%d\n", 500);
exit(0);
 
Old 09-19-2005, 06:42 PM   #3
anekkan
LQ Newbie
 
Registered: Sep 2003
Posts: 5

Original Poster
Rep: Reputation: 0
That works. Thanks.

But the problem is this.

lets say I have multiple printf's. All the prints get passed back into "val"

Anyway of being able to return only the one value.

There are ways around it I guess.
 
Old 09-19-2005, 06:52 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
what are the other printf's for ?
if you want to see them on the screen and not put them in the variable, a simple way to do it is to print on stderr "fprintf(stderr,...", you can also print on /dev/tty.
 
Old 09-19-2005, 07:44 PM   #5
anekkan
LQ Newbie
 
Registered: Sep 2003
Posts: 5

Original Poster
Rep: Reputation: 0
Well those executables are quite complex and print out a number of driver specific information etc.

Basically, what I am trying to do is convert a whole bunch of perl scripts to bourne shell scripts. This sort of thing is easily done in perl by just doing:

$val = system("./prog1")

and you can access the return value through "val"

You may ask why am I not using Perl. Because the target platfrom has very minimal memory and does not support Perl.

But I am not able to access the return value in the bourne shell. Thats the whole problem.

So any other way to do this. Lets say for now that it is not feasible to use printf

The objective is to get the value that is returned from the program by: return 500

Thanks.
 
Old 09-19-2005, 07:50 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
$val = system("./prog1")
This can be done in shell with:
Code:
./prog1
val=$?
As I already wrote, return values are bytes, so your 500 return value example is wrong, either with perl or shell, it doesn't matter.
 
Old 09-19-2005, 07:59 PM   #7
anekkan
LQ Newbie
 
Registered: Sep 2003
Posts: 5

Original Poster
Rep: Reputation: 0
I had tried that. i.e:

./prog1
echo $?

and for some reason it was not picking up the return value earlier. But now it seems to work. I know that sounds a little wierd but....

Thanks a lot for you help.

And I got your point about the return value being stored in a byte. I was just using the value 500 as an example. The return value should be less than 256.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 09:20 AM
redirecting output in Bourne shell trutnev Linux - Software 2 04-22-2004 03:55 AM
Bash Shell vs Bourne infamous41md Linux - Newbie 14 04-11-2003 10:16 PM
Bourne Again SHell help MagInnovision Linux - Newbie 2 02-04-2003 03:43 PM
Bourne shell programming noodle123 Programming 3 04-16-2002 03:46 AM


All times are GMT -5. The time now is 01:49 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration