LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-27-2009, 01:22 AM   #1
wimnat
Member
 
Registered: Dec 2004
Location: Sydney, AU
Distribution: CentOS 5.x, Backtrack
Posts: 72

Rep: Reputation: 16
Very n00b question about exiting a bash script


I SSH to a my machine and run a bash script

Code:
#!/bin/bash
if [ $1==-f ]; then
	force=yes;
else
	force=no;
fi

echo $force
exit;
Why does this exit my ssh session rather than just exiting the bash script?
 
Old 05-27-2009, 01:42 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
1. I'm pretty sure you need a space on each side of '=='
2. as you're not using the 'exit' to rtn a specific final status, just remove it. The script will exit/complete anyway.
 
Old 05-27-2009, 01:54 AM   #3
wimnat
Member
 
Registered: Dec 2004
Location: Sydney, AU
Distribution: CentOS 5.x, Backtrack
Posts: 72

Original Poster
Rep: Reputation: 16
The problem occurs though if I put the statement elsewhere though....

Code:
if [ $force!=yes ]; then
	while ( [ $choice!=yes ] || [ $choice!=no ] ) do
		echo -n "Are you sure you want to continue? (yes/no) "
		read choice
		echo		
		case "$choice" in
			yes) 
				break;
			;;
			no) 
				exit;
			;;
		esac
	done
fi;
Here, if i enter 'no' when prompted, my ssh session will end. If i don't put it in then my script will loop until i enter 'yes'.
 
Old 05-27-2009, 02:14 AM   #4
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
exit == logout, so that's why it ends your session.

What do you want it to do?
 
Old 05-27-2009, 03:18 AM   #5
wimnat
Member
 
Registered: Dec 2004
Location: Sydney, AU
Distribution: CentOS 5.x, Backtrack
Posts: 72

Original Poster
Rep: Reputation: 16
quit the script and return to the console/shell.
 
Old 05-27-2009, 03:30 AM   #6
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Why doesn't break do what you want?
 
Old 05-27-2009, 03:35 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The above code snippets work.

If the script is executed, the break will exit the while loop (and continue with the rest of the script), if the exit clause is executed the script is ended and the prompt returns.

If the script is parsed (!!!!) the exit (any exit) will terminate your shell.

Are you parsing or executing the script?
 
Old 05-27-2009, 07:15 AM   #8
wimnat
Member
 
Registered: Dec 2004
Location: Sydney, AU
Distribution: CentOS 5.x, Backtrack
Posts: 72

Original Poster
Rep: Reputation: 16
Errrr... tbh i don't know the difference!

I run it using...

#>. my_script
 
Old 05-27-2009, 07:36 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You are parsing the script, not executing it.

An explanation in a nutshell:

If you execute a script (./script.sh or just script.sh if it can be found in your PATH) the following will happen:
- A new (child) shell is opened,
- All the command in the script are executed in that (child) shell,
- When the script finishes the (child) shell is closed and control is returned to the (mother) shell.

If you parse a script all command in your script are executed in the (mother) shell, there is no (child) shell.

The exit command will close 'the' shell, depending on how you execute the script the child shell or the mother shell is closed. If, like you did, you parse the script, the moment an exit is encountered the mother shell is closed (the shell you type your commands).

For now: make your script executable (chmod 750 script.sh) and execute it (./script.sh).

Hope this clears things up a bit.
 
Old 05-27-2009, 07:13 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
I agree with billymayday (as usual ). break will do what you want.
 
Old 05-28-2009, 11:59 AM   #11
esoukenka
LQ Newbie
 
Registered: Feb 2009
Posts: 19

Rep: Reputation: 2
Maybe

I am not sure but I always used exit 1 in my bash scripts. Make sure you got the bin/bash prefix in script.

This is only a noobie on this advice though
 
Old 05-28-2009, 08:11 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
Don't use 'exit' unless you want to force the exit value for a reason. In *nix shells & programs generally, the convention is a termination code of 0 (zero) means ok, anything else indicates an error.
By default, if you don't explicitly use eg 'exit 0', then the termination value is that of the last cmd run.

Hence the reason you often see code like
Code:
do_someprog
if [[ $? -ne 0 ]]
then
    echo "error program failed"
    exit 1
fi
 
  


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
Exiting BASH function without exiting sourced environment SwingingSimian Programming 10 08-12-2009 12:59 AM
Bash script question.... mkhan919 Linux - Newbie 3 04-06-2007 06:13 AM
Hi, I am a complete n00b in need of help executing her bash shell script. savoirefaire Linux - Newbie 4 03-12-2007 06:14 AM
N00b: Simple Question about Bash Func and Vars: TylerD75 Programming 6 04-03-2005 06:03 AM
Bash script question deiussum Programming 6 08-14-2004 11:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:50 AM.

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