I’m trying to write a bash script that will run on both Mac and Windows by using win-bash for the windows side, since it doesn’t have to be installed.
However, Mac and Windows seem to have different opinions of syntax.
For example, I have the following script thus far:
echo "Setup..."
shopt -s expand_aliases #make sure aliases work
if [ "$(uname)" == "Darwin" ]; then
alias p4cli=./bin/p4
else
alias p4cli=p4
fi
echo "Checking login status..."
p4cli groups > .trash
if [ $? -ne 0 ]; then
p4cli login
fi
echo "Done!"
This fails with:
[: ==: binary operator expected
On windows (win-bash) but works just fine on Mac (which I’m going to assume is the more correct one… since it’s actually Unix).
Any idea of what I’m doing wrong here?
Try single
=instead of double==:They both mean string equality check inside a test, but the single equal sign for comparison is POSIX-compliant, whereas the double equal sign isn’t – it just works for the current bash / ksh as a syntactic sugar for readability (will fail for older shells as well).