script does not close terminal after running


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script does not close terminal after running
# 1  
Old 09-01-2011
script does not close terminal after running

For a small script i want it so that the terminal closes when the script has completed its tasks.
To do so i use at the end if the script the following:
Code:
echo "Hello, World!" 
echo "Knowledge is power."
echo ""
echo "shutting down terminal in 10 seconds" 
exit 10

however the terminal stay's open.

i tried it to exit immediately (without the 10 after exit)
but that also wont work

tried &exit without result
tried && exit without result

cant find the solution trough google, so i hope somebody can help me here.

thx

(i am trying to learn scripting a bit, and i am fussing around with this one: http://bash.cyberciti.biz/guide/Hello,_World!_Tutorial to make it do other stuff, just to try to understand the scripting more)

Last edited by Franklin52; 09-02-2011 at 03:25 AM.. Reason: Correcting code tags
# 2  
Old 09-01-2011
The "exit 10" just tells the script to return an exit status "10". It does not mean it will close the terminal in 10 seconds.

You can source the script so that it runs in the current shell:

Example:

Code:
[root@atlas tmp]# ps -p $$			## Current shell is bash
  PID TTY          TIME CMD
12755 pts/0    00:00:00 bash

[root@atlas tmp]# ksh				## Change to Korn shell (parent process is bash)
# ps -p$$
  PID TTY          TIME CMD
12809 pts/0    00:00:00 ksh

# /tmp/script.sh				## Runs the script in a subshell
Hello, World!
Knowledge is power.
shutting down terminal in 10 seconds

# ps -p$$					## Upon execution, shell is still KSH
  PID TTY          TIME CMD
12809 pts/0    00:00:00 ksh

# . /tmp/script.sh				## Now force the script to run in current shell
Hello, World!
Knowledge is power.
shutting down terminal in 10 seconds

[root@atlas tmp]# ps -p $$			## KSH got closed. Current shell is now bash.
  PID TTY          TIME CMD
12755 pts/0    00:00:00 bash


Last edited by verdepollo; 09-01-2011 at 02:07 PM.. Reason: Add clarification
# 3  
Old 09-01-2011
say what?
your explanation is probably 100% correct, but i don't understand a thing of it Smilie

but when i just use exit i think it should exit the terminal, or am i that wrong?
# 4  
Old 09-01-2011
Quote:
but when i just use exit i think it should exit the terminal, or am i that wrong?
The shell is closed, not the terminal.

A terminal is -strictly speaking- hardware attached to a computer that provides basic input/output features. This definition however is now pretty much historical as nowadays terminal emulators are much more common and popular (putty, xterm, etc).

Short answer:

Use:
Code:
. ./script

instead of:
Code:
./script

EDIT: Since you're following Nixcraft's tutorials, you might want to have a look at this one for a brief explanaiton on subshells: What is a Subshell? - Linux Shell Scripting Tutorial - A Beginner's handbook

Last edited by verdepollo; 09-01-2011 at 02:46 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Terminal running bash/rsync script does not close with exit (MacOS High SIerra)

Hello, I am running a bash script to do an rsync back on a computer running MacOS High Sierra. This is the script I am using, #!/bin/bash # main backup location, trailing slash included backup_loc="/Volumes/Archive_Volume/00_macos_backup/" # generic backup function function backup {... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

2. UNIX for Advanced & Expert Users

Close UNIX terminal

Hi, In one of the circumstances, my manager asked my password and opened unix terminal (Putty) with my user credentials. Its been more than 2 days that terminal is opened (saw it through finger command). How do I close that terminal which is not opened in my pc. I'm very uncomfortable of the... (11 Replies)
Discussion started by: bobbygsk
11 Replies

3. Shell Programming and Scripting

Running shell script in Cygwin terminal

I am new to shell scripting. I tried to run a simple shell script using Cygwin terminal in Win XP env. The script I have written is as follows - #!/bin/bash a=5 ] && echo "true" || echo "false" But when I execute the script, getting some confusing error. The error I am getting are - ... (3 Replies)
Discussion started by: linux_learner
3 Replies

4. UNIX for Advanced & Expert Users

close a mac terminal

Is there a trick to closing a mac terminal with a command? I would think you could just type exit into your terminal but that doesn't work. I also tried quit and close just for the hell of it and that didn't work either. Does anyone know what the command is? (1 Reply)
Discussion started by: cokedude
1 Replies

5. Shell Programming and Scripting

Usage of NOHUP - How to keep the child process running even if I close the Server connection

Hi. ! When I use the 'NOHUP' along with the '&', the process will be running in the background. Even when I attempt to close (Meaning 'EXIT') the session (say PUTTY in this case), it wont exit unless the process is completed. But, say when I forcefully terminate the session (SHUT DOWN the... (2 Replies)
Discussion started by: WinBarani
2 Replies

6. AIX

User cant log in (close terminal)

Hello everyone I have a user on my server, Aix 5.3 TL9 sp4. Weeks ago I dont have a problem but today the user cannot log in. let me explain. Me with root user I can change his password. then I log in with the user and I can change the password and the terminal close. Im using ssh. But... (2 Replies)
Discussion started by: lo-lp-kl
2 Replies

7. UNIX for Dummies Questions & Answers

Problems running script on remote Terminal

Hi, I'm new here so please excuse any stupidity that occurs in my post :P My situation: Have a java program which I have to run a ridiculous amount of times and put the output data into a text file. Thought the easiest way to do this would be to delve into the world of scripts. I am at home... (1 Reply)
Discussion started by: lozyness
1 Replies

8. Shell Programming and Scripting

running terminal with script

suppose we have a file ab 81 and another file exec < $1 while read line do ssh root@172.16.1.$line done while running the command sh file.sh ab output display as shown Pseudo-terminal will not be allocated because stdin is not a terminal. root@172.16.1.81's password: after... (3 Replies)
Discussion started by: cdfd123
3 Replies

9. Shell Programming and Scripting

Running a script without a terminal session

I'm trying to figure out how I can run a script "myScript.sh" in such a way that if my remote network connection gets disconnected, the script doesn't stop functioning. Right now I log in, run "./myScript.sh" and watch my output get pumped to a log file for about 10 hours. Only problem is that... (3 Replies)
Discussion started by: jjinno
3 Replies
Login or Register to Ask a Question