LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2006, 04:37 AM   #1
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Rep: Reputation: 30
program exit return 0 or 1?


Under linux if a program exit successfully what does it return?

and when it exit with error what is returned.

Also how do you check under bash shell if the previously runned program exit with error or success?
 
Old 07-20-2006, 04:38 AM   #2
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
0 for success
non 0 for others.
 
Old 07-20-2006, 04:39 AM   #3
prozac
Member
 
Registered: Oct 2005
Location: Australia
Distribution: slackware 12.1
Posts: 753

Rep: Reputation: 32
if [ "$?" == "0" ]; then
echo "success!"
else
echo "failed!"
fi
 
Old 07-20-2006, 04:46 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
as well as prozac's answer,

Code:
if command; then
    echo success
else
    echo doh
fi
or
Code:
command && echo "yes" || echo "no"
 
Old 07-20-2006, 05:00 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
there are more other ways

the most common and cleanest way is
Code:
if program; then
    echo 0/true
else
    echo 1/false
fi
you can also do this
Code:
program && echo true
program && {
   more statements if true
}
and also include the false parts like
Code:
program && echo true || echo false
program && {
   more statements if true
} || {
   more statements if false
}
but i don't recommend the last ones since sometimes the false parts are executed when the true statements return a false value

you must also note that your program should return true and false values. some programs only return true unless they are interrupted or are caught by a signal

if a program exits or is suspended because of a signal, the return value is always 128 + signal number

you can do exit codes in a bash script using exit /b # by the way
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call another cgi program to return a variable <db> Programming 3 04-27-2006 03:36 AM
Exit with Return nopcoder Programming 0 12-27-2005 10:03 AM
get exit status from a C-program in Bash Yoko Programming 1 07-12-2005 04:33 PM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
how to check the return value of a program Sammy2ooo Linux - General 1 04-27-2004 02:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration