How to quit from a script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to quit from a script?
# 1  
Old 01-29-2010
How to quit from a script?

hi all,

I am facing problem in shell scripting while using exit command, when ever i run a file using . ./<filename>, when i run the sae script as sh <filename> the script does not close the windows. since my script has function calls i have to use . ./ <filename>.

Could any one tell me where i am going wrong?

Thanks
# 2  
Old 01-29-2010
Post the script here.
# 3  
Old 01-29-2010
When you use 'sh script' you're invoking another shell to run the script while '. ./script' uses the current shell to parse the script.
# 4  
Old 01-29-2010
sorry for the slight delay, had to write a new one to give it to you all. this is in essence the logic of my script.

Code:
function hello
{
echo "testing exit"
res=$?
return $res
}
# main
hello
if [ $res -eq 0 ] ; then
exit 1;
fi


Last edited by pludi; 01-29-2010 at 03:59 AM.. Reason: code tags, please...
# 5  
Old 01-29-2010
The debug code is self explanatory

Code:
% ksh -x hello.sh
+ hello
testing exit
+ [ 0 -eq 0 ]
+ exit 1

sh
Code:
sh -x hello.sh
+ function hello
hello.sh: function: not found
+ echo testing exit
testing exit
res=0
+ return 0
hello.sh: cannot return when not in function

# 6  
Old 01-29-2010
I am well aware of the useage of sh and . ./ when i use exit 1 without a function call and run the script, the script doesnt close the putty session, however when i run the script with a function and now i use . ./ the script closes the putty session.

How could I avoid it?

Thanks.
# 7  
Old 01-29-2010
This is by design. The script is not supposed to close your putty session (it closes its own subshell). If you are sourcing the script (using . ) then it is, since you are calling exit in your current shell environment so it will close that.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. OS X (Apple)

Are you sure you want to quit Safari?

Hmmm. I cannot figure out where to disable this warning message in Safari. Google says to disable something in Safari Tabs preferences but my Macs do not have that option in Mojave. Anyone know how to disable the following so when I quit Safari it simply quits without the "freeze the... (12 Replies)
Discussion started by: Neo
12 Replies

2. Shell Programming and Scripting

Need to quit out of for loop.

Hi, Here is my code to send read.txt to three servers. col="prd167.mybank.com prd168.mybank.com bsprd169.mybank.com" set -A look $col for IndixList in ${look}; do scp /tmp/read.txt admin@$IndixList:/tmp done This works and all the 3 servers gets the read.txt file. However,... (8 Replies)
Discussion started by: mohtashims
8 Replies

3. Shell Programming and Scripting

Opening a file in vi and automatically save and quit this file using shell script

Hi friends, In my shell script, I want to open a file using vi editor. After opening the file in vi, I want to save and quit this file automatically.... all through shell script. the code segment is: ------------------------------------------------------------ cd ~/netfpga/projects/scone/sw/... (2 Replies)
Discussion started by: sachinteotia
2 Replies

4. OS X (Apple)

Quit a shell script thats running on a remote machine

I'm in a situation where I am executing a shell script(Bash) on another machine remotely using ssh, and for various reasons sometimes need to quit it and restart it. The shell script being run does many different things, so its hard to know what process to kill on the remote machine, and even if I... (2 Replies)
Discussion started by: TheDrizzle
2 Replies

5. Shell Programming and Scripting

FTP script - 'quit' never executes

I have wrote a script to get a very large encrypted file from a remote ftp server to a local debian box. The file downloads successfully, but the script never exits, or quits. None of code executes after the ftp get command. The file is approx 291M Here is the code: !/bin/sh... (3 Replies)
Discussion started by: jstrahm
3 Replies

6. Shell Programming and Scripting

if no file then quit

I have a script that run several subscripts. I need to find out how to do two things. First I would like to check for a file and if that file is not there I want to quit the entire script without running the rest of the script which contain subscripts. If the file is there, I want it to continue... (1 Reply)
Discussion started by: libertyforall
1 Replies

7. Shell Programming and Scripting

quit any time

how can i read input to quit any time, for instance "type q to quit" I have a script like this echo "The first choice" read firstChoice echo "The second choice" read secondChoice Looking for a code to quit any time by pressing q to quit any help would be appreciated thanks (5 Replies)
Discussion started by: Qwond
5 Replies

8. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies

9. UNIX for Dummies Questions & Answers

how to quit from glance

hi, if i am in glance, how do i exit? thanks (2 Replies)
Discussion started by: yls177
2 Replies
Login or Register to Ask a Question