Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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ā€¦
Utilizing an array to gracefully append to a list of EmailAddresses
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

707 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