简体   繁体   中英

Exit a shell script properly in Mac OS X

I have a shell script that create for me a project with the full structure, git repository etc... The problem is that I can't use the terminal after the execution of the test. Here is a sample of my script :

read -p "Have you created the remote repository : $repo.git ? [y/n] " ok
if [ $ok != "y" ]; then
    echo "You must create the remote repository before."
    exit 0;
fi

git init
# Rest of the script...

When I type "n" as an answer, the terminal displays this (for non-french people, "Opération terminée" means "End of the operation") :

You must create the remote repository before.
logout

[Opération terminée]

And I can't use it anymore. The only way to use it again is to close the tab and open another one. Maybe the problem is in the exit 0 ? How to exit the script properly ?

Thanks.

Since this runs from the bash_profile, exit 0 actually closes the shell based on its context. Normally, a script is what's calling the function so it exits not the shell. For example, if you run echo "hello world"; exit 0; echo "hello world"; exit 0; directly in the shell then it closes the shell because that's the context where exit occurs.

As suggested by @mklement0, replace exit 0 with return .

Are you backgrounding the function with & at the end of line somewhere? That would work like sleep 1 & and only print that it's done once you press Enter. The output when the process ends depends on the shell, but in Bash 4.2 it looks like this:

$ sleep 1 &
[1] 11166
$ # Just press Enter after more than a second
[1]+  Done                    sleep 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM