简体   繁体   English

为什么我的 Bash 脚本 (OSX) 从终端运行,但双击时却没有?

[英]Why is my Bash script (OSX) running from the Terminal, but not when double clicked?

Here is the full text of my bash script:这是我的 bash 脚本的全文:

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

I have run chmod u+x on the file, and selected Terminal for the default run Application, but when I double click on it, I get a response saying我已经在文件上运行了chmod u+x ,并为默认运行应用程序选择了终端,但是当我双击它时,我得到一个响应说

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

and the process will not continue.并且这个过程不会继续。 When I run it from the Terminal with the command ./GitC.sh, it runs perfectly.当我使用命令 ./GitC.sh 从终端运行它时,它运行得很好。

Thoughts?想法?

The script makes the assumption that the current working directory is the same as the script, and I'm pretty sure that's not the case when activated through double-clicking in Finder .该脚本假设当前工作目录与脚本相同,我很确定通过在Finder 中双击激活时情况并非如此。 It would also cause issues if you invoked the script like this:如果您像这样调用脚本,它也会导致问题:

/path/to/script.sh

You therefore need to ensure it changes to the same directory as the script:因此,您需要确保它更改为与脚本相同的目录:

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

... etc.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 git 从 bash 脚本和终端签出的行为不同 - git checkout from bash script and from terminal behaves differently 为什么阅读挂我的bash脚本? - Why is read hanging my bash script? OSX 终端在 git 存储库中运行缓慢 - OSX terminal running slow in git repositories 为什么在Windows cmd中运行npm install会失败,但是从git bash或vs终端运行时却可以正常工作? - why does npm install fail when run in windows cmd but works fine when run from git bash or vs terminal? 运行docker快速启动终端或在/ git bash shell中运行时,“ bash:fork:重试:无子进程” - “bash: fork: retry: No child processes ” when running docker quick start terminal or in /git bash shell 为什么终端不解析 .bash_profile 的脚本? (目标是让这个脚本运行并显示当前的工作分支) - Why is terminal not parsing script for .bash_profile ? (goal is for this script to run and show the current working branch) 为什么我的集成VS Code Bash终端不使用bash着色? - Why doesn't my integrated VS Code Bash terminal use bash coloring? 如何让我的“终端”命令从vba(在OSX上)的正确文件夹中运行 - How do i get my “terminal” commands to run in the right folder from vba (on OSX) Git - 为什么在删除的文件上运行命令时需要双短划线? - Git - why are double dashes needed when running a command on a deleted file? 在终端中显示 git 用户名的 Bash 脚本 - Bash script to show git username in terminal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM