我有一个shell脚本,它从一个基础目录递归搜索package.json并像这样运行npm test。
find "$parent_dir" -name package.json -maxdepth 2 -execdir npm test \;
我希望以以下形式截取测试失败:
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
所以我可以让 shell 脚本以错误状态退出。
感谢任何帮助。
我有一个shell脚本,它从一个基础目录递归搜索package.json并像这样运行npm test。
find "$parent_dir" -name package.json -maxdepth 2 -execdir npm test \;
我希望以以下形式截取测试失败:
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
所以我可以让 shell 脚本以错误状态退出。
感谢任何帮助。
又过了3年,我也在寻找解决方案。
最后找到了一个方法,如果将来有人需要帮助的话,可以使用shell脚本中的$?命令来返回上一个执行命令的退出状态。例如,如果shell脚本失败,则返回1,而成功则返回0。NPM实现了此返回值机制。
npm run test
if [ $? -eq 0 ]
then
echo "SUCCESS"
else
echo "FAIL"
fi
if ./somecommand | grep -q 'npm ERR!'; then
#what you want to do
# exit executing the script
return
fi
抱歉我来晚了两年,刚刚加入SO社区。