The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Exit out of the Script Command inside a Script jmungai UNIX for Dummies Questions & Answers 6 02-20-2009 05:26 PM
which file is sourced by bash on login (Mac OS X 10.5.3)? Alex_Land OS X (Apple) 2 07-11-2008 11:52 PM
Script exit Krrishv Shell Programming and Scripting 3 06-23-2008 06:27 PM
how to exit a script? filthymonk Shell Programming and Scripting 15 06-29-2007 07:27 AM
exit from script gefa Shell Programming and Scripting 1 01-23-2007 08:02 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-04-2009
angad.makkar angad.makkar is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 16
Question Exit from sourced script

Hello,
I have written a script (say chld.sh). its structure is as follows:

------------------------
#!/bin/sh

usage()
{
echo "chld.sh <param1> <param2>"
exit 0
}

chk_param_1()
{
case $param1 in
c) export PATH=$PATH:/home_a/bin/execs;;
d) export PATH=$PATH:/home_b/bin/execs;;
*) usage;;
esac
}

chk_param_2()
{
echo "Chk Param 2"
}

clear
param1=$1
param2=$2
chk_param_1
chk_param_2
------------------------

Now I source this script, as I want the new PATH variable in my current environment. Hence I give a alias in my environment as
alias set_path='. ~/chld.sh'

Now when I call the script as:
set_path c ........it works fine
set_path d..........also works fine.

But if I give "set_path e"......it displays the usage and EXITS/LOGOUTS from my parent shell i.e. the main shell.

I know the reason is coz due to sourcing, it runs the chld.sh in the current shell and not another subshell.
But please tell me how to do this.

Whenever I display "usage".....i dont want to continue ahead in the script.
Simply come out of this script.

Please HELP!!

Last edited by angad.makkar; 03-04-2009 at 05:49 AM..
  #2 (permalink)  
Old 03-04-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

In a sourced script, use return rather than exit.
  #3 (permalink)  
Old 03-04-2009
angad.makkar angad.makkar is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 16
Well return works amazingly well.

Thanks a lot.

Mods....please close this thread as the answer given is the best!!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0