Solved

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

Posted on 2009-05-06
4
537 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 250 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 250 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

740 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