1

I'm trying to implement a git hook that edits some JSON every time I push. I have JQ installed on my Mac using homebrew "brew install jq", but when the git hook runs my .sh I get the error

jq: command not found

My latest attempts have been to use curl to download the jq library, point to it, and run jq that way:

jq=/usr/local/Cellar/jqz
curl -L -o $jq https://github.com/stedolan/jq/releases/latest/download/jq-osx-amd64

Unfortunately, this is also returning the same 'command not found' error.
Sidenote: jq=/usr/bin/jq gives me a permission error when I try to write to it

  • 1
    A file directly created by `curl` won't have execute permissions unless you use `chmod` to add them. – Charles Duffy Feb 01 '21 at 22:34
  • BTW, this isn't particularly software-development-specific; you'd have the same problem installing _any_ executable the same way, including one that wasn't a development tool or being used in a development-specific context. As such, I'd consider [apple.se] likely to be a more topical place. – Charles Duffy Feb 01 '21 at 22:34
  • 1
    Anyhow -- make sure the context in which your hook runs has a `PATH` that includes the location with your executable. – Charles Duffy Feb 01 '21 at 22:35
  • (...I also recommend using Nix instead of Homebrew, but that's an entirely different discussion). – Charles Duffy Feb 01 '21 at 22:35
  • ...and yes, it's completely normal that you can't write to `/usr/bin` on Catalina and later; that filesystem is locked-down and read-only on current OS releases. – Charles Duffy Feb 01 '21 at 22:36
  • 1
    (also, "git bash" isn't a thing that _exists_ on MacOS, so it's odd to hear you use the term; "Git Bash" is the name for bash _as packaged with Git for Windows_; it doesn't exist on non-Windows platforms, where what you have is just "bash", not "git bash"). – Charles Duffy Feb 01 '21 at 22:39
  • jq=/usr/local/Cellar/jqz curl -L -o $jq https://github.com/stedolan/jq/releases/latest/download/jq-osx-amd64 chmod -x $jq bash files are not my wheelhouse, I'm just trying to hack together something to auto-version my project when I commit. I'm currently searching for how to use chmod to add this executable... – user15126098 Feb 01 '21 at 22:50
  • `chmod +x jq` makes `jq` be flagged executable. Note that it's `+` not `-` to add (instead of removing) the permission. – Charles Duffy Feb 01 '21 at 23:02
  • Also, when you're putting code in a comment, be sure you add `;`s as line separators. Lots of things don't work in bash when you leave out command separators (`foo=bar baz` and `foo=bar; baz` -- the latter being the same if you have a newline in place of the `;` -- are both syntactically valid, but they do completely different things) – Charles Duffy Feb 01 '21 at 23:03
  • As another aside, be sure you quote parameter expansions. `"$jq"`, with the quotes, not just `$jq`. The bugs won't bite you 100% of the time (depends on your data, the current value of IFS, and sometimes what files are in your current directory), but when they do, they can sting. – Charles Duffy Feb 01 '21 at 23:04

1 Answers1

0
jq=/usr/local/Cellar/jqz
curl -L -o $jq https://github.com/stedolan/jq/releases/latest/download/jq-osx-amd64

It looks like you are storing the binary with the name jqz. No surprise that it cannot be executed as jq; you would have to invoke it as jqz.

I don't know if /usr/local/Cellar is part of your PATH?

The canonical way would be:

jq='/usr/local/bin/jq'
curl -L -o "$jq" https://github.com/stedolan/jq/releases/latest/download/jq-osx-amd64

you could also store it in the bin directory of your home folder: `jq="$HOME/bin" which should be added automatically to your PATH on most installations (might require a logout & login).

knittl
  • 246,190
  • 53
  • 318
  • 364