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:
- The first one give an empty value.
- 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.
OUTPUT=$(which port 2>&1)followed byecho $OUTPUTgives mewhich: no port in ($PATH). Note "no port in", not "port not found". In any case,OUTPUTis not empty for me.