LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Very n00b question about exiting a bash script (https://www.linuxquestions.org/questions/linux-software-2/very-n00b-question-about-exiting-a-bash-script-728759/)

wimnat 05-27-2009 01:22 AM

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?

chrism01 05-27-2009 01:42 AM

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.

wimnat 05-27-2009 01:54 AM

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'.

billymayday 05-27-2009 02:14 AM

exit == logout, so that's why it ends your session.

What do you want it to do?

wimnat 05-27-2009 03:18 AM

quit the script and return to the console/shell.

billymayday 05-27-2009 03:30 AM

Why doesn't break do what you want?

druuna 05-27-2009 03:35 AM

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?

wimnat 05-27-2009 07:15 AM

Errrr... tbh i don't know the difference!

I run it using...

#>. my_script

druuna 05-27-2009 07:36 AM

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.

chrism01 05-27-2009 07:13 PM

I agree with billymayday (as usual :) ). break will do what you want.

esoukenka 05-28-2009 11:59 AM

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 :)

chrism01 05-28-2009 08:11 PM

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 03:07 AM.