简体   繁体   中英

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

Here is the full text of my bash script:

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM