3

I'm writing a shell script and I need to pull something from github using the command go get github.com/aktau/github-release, but I first need to make sure that the person calling the script has GO installed on their machine. If they do not have it installed, my script will error out and tell them to install GO and then rerun the script.

I was thinking that I should just echo $GOPATH, but people can have GO installed without having their GOPATH defined.

How should I go about doing this?

hellyale
  • 143

1 Answers1

1

You should check the PATH environment variable. See this answer for a detailed explanation.

Where bash is your shell/hashbang, consistently use hash (for commands) or type (to consider built-ins & keywords). When writing a POSIX script, use command -v.

$ command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }
$ type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }
$ hash foo 2>/dev/null || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }
Tran
  • 126