?
Solved

How to capture the return value from main in a Shell script

Posted on 2009-05-06
4
Medium Priority
?
543 Views
Last Modified: 2012-05-06
I have a C program which is returning an integer from main, and based on that integer I then need to decide which awk script to run. Is there a way for me to capture the integer returned by main?
I am trying to do things like this in my 'controlling' shell script

set choice = ./myProgram

but when I do this, myProgram does not run...

Can anyone help?

0
Comment
Question by:AndreeaN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 1000 total points
ID: 24312627
Check the variable $? as that should have the return value from main.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 1000 total points
ID: 24312649
Do something like
#!/bin/sh
/path/to/myprog
 
case $? in
1)  echo "action for exit status of 1"
    ;;
2)  echo "action for exit status of 2"
    ;;
*)  echo "Unknown exit status"
    ;;
esac

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24312652
if ./myProgram
then
  awk program_succeded
else
  awk program_failed
fi
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24313145
try this:

res=`/path/to/my/prog`
case $res in
1)  echo "action for exit status of 1"
    ;;
2)  echo "action for exit status of 2"
    ;;
*)  echo "Unknown exit status"
    ;;
esac

I modifies Tintin's code. You may add other case as required
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question