在Mac OS X中正确退出shell脚本

时间:2014-04-25 15:15:41

标签: macos shell

我有一个shell脚本,它为我创建了一个具有完整结构,git存储库等的项目...... 问题是我在执行测试后无法使用终端。以下是我的脚本示例:

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

当我输入“n”作为答案时,终端显示此信息(对于非法国人,“操作终结”表示“操作结束”):

You must create the remote repository before.
logout

[Opération terminée]

我不能再使用它了。再次使用它的唯一方法是关闭标签并打开另一个标签。也许问题出在exit 0?如何正确退出脚本?

感谢。

2 个答案:

答案 0 :(得分:6)

由于这是从bash_profile运行的,exit 0实际上是根据其上下文关闭shell。通常,脚本是调用函数的函数,因此它不会退出shell。例如,如果直接在shell中运行echo "hello world"; exit 0;,那么它将关闭shell,因为这是退出发生的上下文。

根据@ mklement0的建议,将exit 0替换为return

答案 1 :(得分:0)

你是否在某个地方的末尾以&为背景功能?这将像sleep 1 &一样工作,并且只有在按Enter键后才打印它。进程结束时的输出取决于shell,但在Bash 4.2中它看起来像这样:

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