我对shell脚本(使用bash)非常不好,我正在寻找一种方法来检查当前git分支是否为“ x”,如果不是“ x”,则中止脚本。
#!/usr/bin/env bash
CURRENT_BRANCH="$(git branch)"
if [[ "$CURRENT_BRANCH" -ne "master" ]]; then
echo "Aborting script because you are not on the master branch."
return; # I need to abort here!
fi
echo "foo"
但这不是很正确
使用git rev-parse --abbrev-ref HEAD
来获得当前分支的名字。
然后,只需比较脚本中的值即可:
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [[ "$BRANCH" != "x" ]]; then
echo 'Aborting script';
exit 1;
fi
echo 'Do stuff';
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句