LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-22-2005, 12:18 PM   #1
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Rep: Reputation: 15
How to get exit code of a progarm in Shell?


If want to know how to get the exit code of a program in shell scripts. Thanks.
 
Old 09-22-2005, 12:56 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
The exit code is stored in a special variable: $?

I would suggest storing the exit status in a variable immediately after executing the command to avoid any confusion. For instance:
Code:
#!/bin/bash

ls /opt > /dev/null
exit_status=$?

if [ ${exit_status} -eq 0 ] ; then
  echo "ls command completed successfully"
else
  echo "ls failed; exit status of ${exit_status}"
fi
 
Old 09-22-2005, 01:46 PM   #3
neo_in_matrix
Member
 
Registered: Sep 2003
Posts: 52

Original Poster
Rep: Reputation: 15
Wow, thanks!
 
  


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
Exit Code: 7 dbzw SUSE / openSUSE 0 12-21-2004 01:06 PM
exit code 2 -- pppd eduac Linux - Software 3 11-21-2003 05:19 AM
atexit and exit code GŠutama Programming 3 11-01-2003 02:15 AM
pppd exit code = 10 eduac Linux - Hardware 1 10-20-2003 02:57 AM
kppp - exit code 1 grim1234 Slackware 3 09-02-2003 09:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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