【问题标题】:Why is my Bash script (OSX) running from the Terminal, but not when double clicked?为什么我的 Bash 脚本 (OSX) 从终端运行,但双击时却没有?
【发布时间】:2016-07-28 08:42:39
【问题描述】:

这是我的 bash 脚本的全文:

GitC.sh

#!/bin/bash

git add .
git status
echo “Are you sure you want to commit and push? y/n”
read yn
if [ “$yn”==“y” ];
    then
    echo “Message:“ 
    read m
    git commit -m “$m”
    git push
    sleep 5s
else
    git reset
    sleep 5s
fi

我在文件上运行了chmod u+x,并选择了终端作为默认运行应用程序,但是当我双击它时,我得到一个回复​​说

justins-mbp:~ Schwaitz$ /Users/Schwaitz/Desktop/JustWas/GitC.sh ;退出;

并且该过程将不会继续。当我使用命令 ./GitC.sh 从终端运行它时,它运行良好。

想法?

【问题讨论】:

  • 您不需要.command 后缀来在finder 中运行bash 脚本吗?
  • 也许是一个剪切和粘贴问题,但是你的花引号看起来很可疑,并且你在 if 语句中缺少“==”周围的空格(顺便说一句,最好只写“=”而不是“== " 符合 POSIX 标准)。

标签: git macos bash shell if-statement


【解决方案1】:

脚本假设 当前工作目录 与脚本相同,我很确定通过在 Finder 中双击激活时不会出现这种情况时间>。如果您像这样调用脚本也会导致问题:

/path/to/script.sh

因此,您需要确保它更改为与脚本相同的目录:

dir="$(dirname "$0")"
cd "$dir"

... etc.

【讨论】:

  • 问题是,我的 GitC.sh 文件和我的 git repo 在同一个目录中。
  • @kuuy 没关系;我的答案应该适用于那种情况。
猜你喜欢
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 2023-04-04
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多