【问题标题】:OSX: Why does exit work when written manually in the terminal, but not with a shell script I run from the terminal?OSX:为什么在终端中手动编写时退出工作,而不是我从终端运行的 shell 脚本?
【发布时间】:2015-02-28 02:56:32
【问题描述】:

osx 中的这个 bash 脚本不会退出脚本。我已经更改了设置(如建议的here 那样,在终端中写入 exit 会关闭窗口,并且我已经检查过如果我在终端中写入 exit 会关闭终端窗口,这可能是什么原因?更改设置后,我重新启动了终端和我的 mac,看看是否解决了问题。但是在位置 nr 1 或脚本末尾退出不起作用。

#!/bin/bash
PATH=/opt/local/bin:/opt/local/sbin:$PATH
read -p "What do you want to do?"
    if test "$pass" = "f"
    then
        sh ~/f.sh
        exit # nr 1
    fi
     if test "$pass" = "s"
     then
         sh ~/s.sh
         exit # nr 1
     fi
     exit # nr 2

【问题讨论】:

  • 你的脚本没有退出还是终端没有退出?如果脚本退出并且当您从 shell 提示符运行脚本时终端没有退出,那是因为它只退出脚本而不是正在运行的 shell 会话。获取脚本 (. script.sh) 而不是运行它 (./script.sh),您将退出正在运行的 shell。

标签: macos bash shell terminal exit


【解决方案1】:

假设你的意思是当你这样做时:

$ exit

您的终端退出。

但是当你运行时

$ ./script.sh

您的终端没有退出。

答案是您的脚本正在其自己的 shell 进程中运行,因此当它退出时,它并没有从终端中运行的 shell 中退出。

如果你使用

$ . script.sh

然后您将在当前 shell 进程中运行脚本,因此exit 将退出正在运行的 shell 并导致终端退出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-18
    • 2021-06-08
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    相关资源
    最近更新 更多