Solved

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

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Acrinis True image 2 118
Determine Who is Runnig my Bash Shell Script 4 91
Loading flat file data in tables 2 99
Windows 10 14 36
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

734 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