我必须使用多个 git 存储库,并且我希望能够将它们全部切换到某个分支。到目前为止,由于我对 bash 脚本的了解有限,我做了这个:
# Git switch all repos to a given branch.
switchall() {
# Reset OPTIND so we can call this function multiple times.
local OPTIND
while getopts "b:" OPTION
do
case $OPTION in
b)
echo "Switching all GIT repo's to: $OPTARG"
for d in ~/git/*/
do
( cd $d && git checkout $OPTARG )
done
;;
esac
done
}
但是假设我想将所有存储库更改为某个分支,但其中一些存储库没有我想要更改为的特定分支。我会收到这样的错误:
error: pathspec 'test' did not match any file(s) known to git.
当我运行时会发生这种情况:
switchall -b test
如何捕获错误消息并抑制它或根据已输出的特定错误消息执行某些操作?
最佳答案
类似的事情:
( cd $d && git checkout $OPTARG 2>/dev/null )
if ! [ $? -eq 0 ]
then
echo things went wrong
fi
甚至:
if ! ( cd $d && git checkout $OPTARG 2>/dev/null )
then
echo things went wrong
fi
正如在特殊参数
部分下的man bash
中一样:
? Expands to the exit status of the most recently executed foreground pipeline.
关于git - 如何响应 bashrc 函数的 git 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40461745/