bash - 为什么这个脚本可以在机器上运行一个而不是另一个?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"
解决方案
我没有看到任何语法错误,但您可以完全消除[[
。
例如,
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
推荐阅读
- macos - 术语“Set-AzureADGroup”未被识别为 cmdlet 的名称
- python - Web Scraping yelp 评论评级 Python
- javascript - 我正在尝试从特定列中获取值
- firebase - 当它不为空时调用空错误的方法
- javascript - 确定何时在传单地图上发生两指地图拖动
- java - Springboot项目在弹性beantalk上找不到404
- javascript - UnhandledPromiseRejectionWarning:错误:交易已被 EVM 还原 - BEP20 Pancakeswap
- python - 新 Stripe PaymentElement 中的 FPX 元素未在前端配置
- javascript - 如何显示两个反应组件
- android - 在应用程序 android 中下载和查看 PDF 文件