1

Environment:

Mac OS Catalina 10.15

GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)

Problem:

I have a bash script.

I try to test if MacPOrt is installed.

When I run the command "port -v", it open Macport terminal and the rest of my script is broken.

So I try another approach with the command "which port". It gives the output "port not found" but I don't succeed to store it in a variable. My variable is empty.

You can see here below the source code of my bash script executing 2 commands:

  1. The first one give an empty value.
  2. The second one give a value if Macport is not installed. But if it is installed, it open the Macport terminal and break my code.
OUTPUT=$(which port 2>&1)

echo "OUTPUT is $OUTPUT"

if echo "$OUTPUT" | grep -q "port not found"; then

    echo "MacPort is NOT installed"

else

    echo "MacPort is installed"

fi

echo "======================================================================"

OUTPUT=$(port -v 2>&1)

echo "OUTPUT is $OUTPUT"

if echo "$OUTPUT" | grep -q "command not found"; then

    echo "MacPort is NOT installed"

else

    echo "MacPort is installed"

fi

Here is the output:

OUTPUT is 
MacPort is installed
======================================================================
OUTPUT is test.sh: line 11: port: command not found
MacPort is NOT installed

So it seems 'which' command doesn't behave like other commands. How can I store the output of 'which' command? If it is not possible, I am agree to accept any other trick to test of Macport is installed.

1
  • Cannot reproduce (although not on MacOS here). OUTPUT=$(which port 2>&1) followed by echo $OUTPUT gives me which: no port in ($PATH). Note "no port in", not "port not found". In any case, OUTPUT is not empty for me. Commented Sep 9, 2020 at 8:58

2 Answers 2

2

If what you're really looking for is just to check if the port command is available, the right way to do it in bash would be:

if ! command -v port > /dev/null; then
  echo "MacPort is not installed"
  exit 1
fi
Sign up to request clarification or add additional context in comments.

Comments

1

Your script has bugs:

OUTPUT=$(which port 2>&1)

echo "OUTPUT is $OUTPUT"

if echo "$OUTPUT" | grep -q "port not found"; then
    echo "MacPort is NOT installed"
else
    echo "MacPort is installed"
fi

You are checking for port not command. But which command doesn't actually output anything if port isn't found (whether which outputs anything differs on different systems). This is seen in your output (OUTPUT is empty).

In any case, you can't rely on which as it may not output anything in some versions and even its return code isn't reliable on some old platforms.

A better approach is to use command or type built-ins of bash (This also avoids calling an external command like which):

if type port &>/dev/null; then
    echo "MacPort is installed"
else
    echo "MacPort is NOT installed"
fi

if command -v port &>/dev/null; then
    echo "MacPort is installed"
else
    echo "MacPort is NOT installed"
fi

Comments

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.