首页 > 解决方案 > 为什么这个脚本可以在机器上运行一个而不是另一个?macOS 卡塔利娜

问题描述

我有这个在我的机器上运行良好的 shell 脚本。为了测试脚本,我有一个具有相同 OS 版本 MacOSCatalina 的 VM。在“真机”上,脚本运行良好,但在 VM 上总是出现错误:

syntax error in a conditional expression, syntax error near ]]

这是脚本:

echo "check if Homebrew needs to be installed" 
echo "Homebrew is a package manager for macOS"
sleep 2
which -s brew
if [[ $? != 0 ]]
then
    echo "Homebrew is not installed, installing Homebrew now"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
    echo "Homebrew is already installed"
fi
sleep 2
echo ""
echo "check if node.js needs to be installed"
echo "node.js is a needed library for angular"
sleep 2
which -s node
if [[ $? != 0 ]]
then
    echo "node.js is not installed, installing node.js now"
    brew install node
else
    echo "node.js is already installed"
fi
sleep 2
echo ""
echo "check if Angular needs to be installed"
echo "Angular as a huge framework serves as the frontend in our project"
sleep 2
which -s ng
if [[ $? != 0 ]]
then
    echo "Angular is not installed, installing Angular now"
    npm install angular
else
    echo "Angular is already installed"
fi
echo ""
echo "Done"

标签: bashmacosshellshvirtual-machine

解决方案


我没有看到任何语法错误,但您可以完全消除[[

例如,

if ! which -s brew
then
    echo "Homebrew is not installed, installing Homebrew now"
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
else
    echo "Homebrew is already installed"
fi

推荐阅读