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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-27-2009, 01:22 AM
|
#1
|
Member
Registered: Dec 2004
Location: Sydney, AU
Distribution: CentOS 5.x, Backtrack
Posts: 72
Rep:
|
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?
|
|
|
05-27-2009, 01:42 AM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
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.
|
|
|
05-27-2009, 01:54 AM
|
#3
|
Member
Registered: Dec 2004
Location: Sydney, AU
Distribution: CentOS 5.x, Backtrack
Posts: 72
Original Poster
Rep:
|
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'.
|
|
|
05-27-2009, 02:14 AM
|
#4
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep:
|
exit == logout, so that's why it ends your session.
What do you want it to do?
|
|
|
05-27-2009, 03:18 AM
|
#5
|
Member
Registered: Dec 2004
Location: Sydney, AU
Distribution: CentOS 5.x, Backtrack
Posts: 72
Original Poster
Rep:
|
quit the script and return to the console/shell.
|
|
|
05-27-2009, 03:30 AM
|
#6
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep:
|
Why doesn't break do what you want?
|
|
|
05-27-2009, 03:35 AM
|
#7
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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?
|
|
|
05-27-2009, 07:15 AM
|
#8
|
Member
Registered: Dec 2004
Location: Sydney, AU
Distribution: CentOS 5.x, Backtrack
Posts: 72
Original Poster
Rep:
|
Errrr... tbh i don't know the difference!
I run it using...
#>. my_script
|
|
|
05-27-2009, 07:36 AM
|
#9
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
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.
|
|
|
05-27-2009, 07:13 PM
|
#10
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
I agree with billymayday (as usual ). break will do what you want.
|
|
|
05-28-2009, 11:59 AM
|
#11
|
LQ Newbie
Registered: Feb 2009
Posts: 19
Rep:
|
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
|
|
|
05-28-2009, 08:11 PM
|
#12
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,397
|
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
|
|
|
All times are GMT -5. The time now is 08:50 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|