3

Without using brew, how can I detect if coreutils is installed on the mac where my bash script is running.

here is my script:

#!/usr/bin/env bash

check_bash_version () {
  echo "checking which bash version is installed...."
  sleep 3
  echo "Bash versioncheck completed"
  sleep 1
  if [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then
    echo "You need to be using Bash version =< 4. Please upgrade"
    exit 1
  fi
}

check_coreutils () {
  echo "checking if coreutils is installed...."
  sleep 3
  # what goes here
}

main() {
  # Main function.

  # check if MacOS
  if [[ "$(uname)" == "Darwin" ]]; then
    check_bash_version
    check_coreutils
  fi
}
3
  • 1. Pick a tool from coreutils. 2. Output --version of this tool. 3. Check if the output has the string GNU in it. Commented May 13, 2020 at 5:58
  • Thanks @KamilCuk. If you make your comment an answer, then I can accept Commented May 13, 2020 at 6:05
  • 1
    Unrelated. Your error message should be You need to be using bash version >= 4. Commented May 13, 2020 at 8:09

1 Answer 1

0

Usually the existence of GNU tools is done by using the output of --version from the tool and checking if it has the string GNU in it.

Sign up to request clarification or add additional context in comments.

2 Comments

This works only if you actually replace mac's old BSD version of coreutils with the GNU ones. If you install GNU coreutils via homebrew, they do not replace them but are installed on a side, in brew's folders and are called by the binary name prefixed with g, so e.g. gcomm instead just comm. So if e.g. comm --version does not return GNU string you should also check gcomm --version.
If you wish to automatically use the installed versions instead of the default ones you can execute the following: echo 'export PATH=\"/usr/local/opt/coreutils/libexec/gnubin:\$PATH\"' >> ~/.bash_profile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.