[英]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.