Very n00b question about exiting a bash script
I SSH to a my machine and run a bash script
Code:
#!/bin/bash |
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. |
The problem occurs though if I put the statement elsewhere though....
Code:
if [ $force!=yes ]; then |
exit == logout, so that's why it ends your session.
What do you want it to do? |
quit the script and return to the console/shell.
|
Why doesn't break do what you want?
|
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? |
Errrr... tbh i don't know the difference!
I run it using... #>. my_script |
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. |
I agree with billymayday (as usual :) ). break will do what you want.
|
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 :) |
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 |
All times are GMT -5. The time now is 03:07 AM. |