0

I have a bash script that runs my Go program. That's all it does, and when I run it from the command line, it works fine.

But when I run it by double clicking on it in Finder, it returns /Users/colin/go/metgen/metaphorgenerator.sh: line 2: ./binary: No such file or directory

So I made it echo it's working directory, and it just prints /Users/colin, my home directory.

How do I get it to run the code from the directory the file is in? (I want it to work no matter what directory it's in)

SemiCoder
  • 3
  • 1
  • 3

2 Answers2

1

You need to point it to the location of the executable, either with a relative path from the working directory as in go/metgen/binary, absolute path like /Users/colin/go/metgen/binary, or absolute path based on the parent executable (unfortunately not reliable).

l0b0
  • 55,365
  • 30
  • 138
  • 223
0

$0 is the full program name. So you can get the directory with HERE=$(dirname "$0").

Then, line 2 should have ${HERE}/binary.

Jack
  • 5,801
  • 1
  • 15
  • 20