LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-03-2010, 05:02 AM   #1
action_owl
Member
 
Registered: Jan 2009
Location: 127.0.0.1
Distribution: Fedora, CentOS, NetBSD
Posts: 115

Rep: Reputation: 17
Bash, using exit status for decisions


How exactly can I use the exit status of a command in an if statement ?

Something like this:
Code:
#!/bin/bash

this=that

if [ $(whatis $this) ]; then

        echo "I know what $this is"

else
	echo "I don't know what $this is"

fi
 
Old 03-03-2010, 05:17 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Two ways. One explicit:
Code:
#!/bin/bash
command
if [ $? -eq 0 ]
then
  echo command successful
else
  echo command not successful
fi
one implicit
Code:
#!/bin/bash
if command
then
  echo command successful
else
  echo command not successful
fi
Note that even [ is a command.

Last edited by colucix; 03-03-2010 at 05:28 AM. Reason: Added note
 
Old 03-03-2010, 05:18 AM   #3
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,

The exit status of a program is held in the following variable: $?

If the content of that variable is 0 (zero) it means all is OK. If it is anything else than that something is "wrong" (if it is actually wrong depends on the program and the number given, you need to check the manpage of that program to see what the number means).

Try this:
Quote:
$ ls -l /etc/fstab
-rw-r--r-- 1 root root 2799 Jan 25 16:23 /etc/fstab
$ echo $?
0

$ ls -l /etc/dummy
ls: /etc/dummy: No such file or directory
$ echo $?
2
About your example:
Code:
#!/bin/bash

this="that"
#this="perl"

whatis $this 1>/dev/null 2>&1
ExitStatus="$?"

if [ $ExitStatus == "0" ]; then
  echo "I know what $this is"
else
  echo "I don't know what $this is"
fi
Hope this helps.
 
Old 03-03-2010, 05:20 AM   #4
action_owl
Member
 
Registered: Jan 2009
Location: 127.0.0.1
Distribution: Fedora, CentOS, NetBSD
Posts: 115

Original Poster
Rep: Reputation: 17
Thank you
 
  


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
Bash Exit Status and Scripts hawk__0 Linux - Software 11 10-06-2009 07:41 PM
Decisions, decisions What Linux Server to use? Electronchaser Linux - Newbie 7 10-06-2009 07:11 PM
Exit status vedanu Programming 4 06-23-2007 05:13 PM
Bash command exit status humbletech99 Programming 12 07-06-2006 12:20 PM
get exit status from a C-program in Bash Yoko Programming 1 07-12-2005 04:33 PM

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

All times are GMT -5. The time now is 03:45 PM.

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