LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 07-23-2008, 02:51 PM   #1
JimRobinson
LQ Newbie
 
Registered: Jul 2008
Posts: 5
Blog Entries: 1

Rep: Reputation: 0
Exit vs Return


I can't find out why exit is preferred over return to terminate a batch process. I can see where the trap processing can be useful using return but I think exit utilizes it as well.

Am I safe to continue using return instead of exit?
 
Old 07-23-2008, 06:18 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
From the main code line, they are the same. There is an implicit exit after returning from the main code line.

Use return in functions. Use exit to explicitly exit from anywhere with a status code.
 
Old 07-24-2008, 06:51 AM   #3
JimRobinson
LQ Newbie
 
Registered: Jul 2008
Posts: 5
Blog Entries: 1

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Mr. C. View Post
From the main code line, they are the same. There is an implicit exit after returning from the main code line.

Use return in functions. Use exit to explicitly exit from anywhere with a status code.
Thanks Mr. C. I appreciate the input.
I typically use "return 1" to abend my batch processes and "return 0" to complete the process. return and exit will perform the same in my case right? I ask because out of habit I have changed alot of the existing code from exit to return 1.
 
Old 07-24-2008, 12:26 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
So perhaps we need to clarify what you mean by "batch process".


Consider the following script and output:

Code:
$ cat hello_world
#!/bin/bash

echo Hello World
return 0

$./hello_world 
Hello World
./hello_world: line 4: return: can only `return' from a function or sourced script
$ echo $?
1
but if we change the interpreter to #!/bin/sh
Code:
$ cat hello_world 
#!/bin/sh

echo Hello World
return 0
$ ./hello_world 
Hello World
$ echo $?
0
We see that sh allows return to be the same as exit at the end of the main code line. Now let's run it in the way I think you mean by "batch":

Code:
$ bash ./hello_world 
Hello World
./hello_world: line 4: return: can only `return' from a function or sourced script
$ echo $?
1
$ sh ./hello_world 
Hello World
$ echo $?
0
Same thing. The final form of executing the shell scripting code is via the . (dot) source command:

Code:
$ . ./hello_world 
Hello World
 $ echo $?
0
Now, change the return to an exit, and try each of the above tests. I'll leave that to you to test out. But this will be the thing that makes your choice clear, so be sure to actually perform the three execution variants. And then the man page segment shows:
.

Code:
       return [n]
              Causes  a function to exit with the return value specified by n.
              If n is omitted, the return status is that of the  last  command
              executed  in the function body.  If used outside a function, but
              during execution of a script by  the  .   (source)  command,  it
              causes the shell to stop executing that script and return either
              n or the exit status of the last  command  executed  within  the
              script  as  the  exit  status  of the script.  If used outside a
              function and not during execution of a script by .,  the  return
              status is false.  Any command associated with the RETURN trap is
              executed before execution resumes after the function or  script.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Warning: at kernel/exit.c:814 do exit() (Tainted: P ) <- nVidia video problem? Hitboxx Fedora 2 10-06-2007 09:47 PM
dhcpcd in Slackware 11.0 does not return any exit codes other than 0? Ken-ji Slackware 1 04-01-2007 06:55 PM
Exception Handling (difference between strderr, exit, abort, return) Darkhack Programming 1 02-27-2007 03:36 PM
program exit return 0 or 1? blackzone Programming 4 07-20-2006 05:00 AM
Exit with Return nopcoder Programming 0 12-27-2005 10:03 AM

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

All times are GMT -5. The time now is 12:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration