LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-26-2009, 07:55 AM   #1
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
$? - description


I was searching for a little if...fi command to check if a fs was already mounted and i stumbled on this:

Code:
cat /proc/mounts | grep /mnt/mymountpoint > /dev/null
if [ $? -eq 0 ] ; then
  echo The path /mnt/mymountpoint is mounted
fi
now what i dont understand is what does $? means in this case?
i have seen $(bash command) or $1, $VARIABLE...

Im fresh in this stuff so please bear with me here.

thanks!
 
Old 07-26-2009, 08:14 AM   #2
chigurh8
Member
 
Registered: Jul 2009
Distribution: Ubuntu, Gentoo
Posts: 102

Rep: Reputation: 18
$? is the exit status of the last executed program.
 
Old 07-26-2009, 08:19 AM   #3
chigurh8
Member
 
Registered: Jul 2009
Distribution: Ubuntu, Gentoo
Posts: 102

Rep: Reputation: 18
You'll get a 0 if grep finds it, and a 1 if it doesn't.
 
Old 07-26-2009, 08:35 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Just a minor addition to the above post:

Grep can also have an exit status that is 2......

Exit status grep:
0 -> found something,
1 -> found nothing,
2 -> an error occured.
 
Old 07-26-2009, 09:01 AM   #5
chigurh8
Member
 
Registered: Jul 2009
Distribution: Ubuntu, Gentoo
Posts: 102

Rep: Reputation: 18
Thanks. Apologies for double posting, also ... it won't be happening again.
 
Old 07-26-2009, 09:16 AM   #6
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
thanks for the info guys, does all programs have the same exit status? or is it dependent on the program itself
 
Old 07-26-2009, 09:39 AM   #7
chigurh8
Member
 
Registered: Jul 2009
Distribution: Ubuntu, Gentoo
Posts: 102

Rep: Reputation: 18
As far as I know, a little of both.
 
Old 07-26-2009, 09:48 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

In general:
If the exit status is 0 (zero) all is ok. This is standard for all the progs/commands that I know of.
If the exit status is anything else, something is up. You need to look in the manpage of the specific command to see what has happened.

By the way, in some cases a none 0 exit status can be ok, grep being an example of that. It's nice that the developers of grep included an exit status of 1 to indicate that nothing is found, but technically this is not an error. It could even be the result you are looking for.

Hope this clears things up a bit.
 
Old 07-26-2009, 10:26 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
AIUI, there are some standardized error/exit status codes that are used for common situations, but in the end it's mostly up to the programmer(s) to define what events produce what codes. It may also depend on what programming language you're using.

For bash scripting, see here and here for more information.

Last edited by David the H.; 07-26-2009 at 11:09 AM. Reason: minor revision to clarify some things
 
Old 07-26-2009, 12:05 PM   #10
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
thanks to all for the infos, everything is clear now.
 
  


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
Is there any C++ api available to know the OS description? matrixdipu Programming 2 07-06-2009 09:04 AM
ls column description anjanesh Linux - Newbie 5 01-04-2006 04:45 AM
Sendmail description samd Linux - Software 4 07-09-2005 11:05 PM
logrotate description pharmhand02 Linux - Software 5 08-02-2004 09:18 AM
/etc/services description gmarais Linux - General 4 03-03-2004 04:10 AM

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

All times are GMT -5. The time now is 05:23 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