Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now