我正在一个bash脚本中使用github cli在某些条件下创建一些repos,但为此,我首先需要检查用户是否安装了cli (我得到了这个检查工作),以及它是否已登录。gh-cli为此提供了一个函数gh auth status,但我不能使用它的返回来检查用户是否已登录。有两种可能的产出:
You are not logged into any GitHub hosts. Run gh auth login to authenticate.或
github.com
✓ Logged in to github.com as <username> (oauth_token)
✓ Git operations for github.com configured to use ssh protocol.
✓ Token: *******************因此,我的第一次尝试是检查该字符串中是否存在“未登录”,其中:
if [[ $(gh auth status) =~ "not logged" ]] ;
then
echo "Not logged" ;
exit 1
fi
#run script但我没有成功。我也尝试使用grep、-oP和sed,但是后来我意识到我无法以字符串的形式获得输出
有人对如何检查或修复这个问题有什么建议吗?
发布于 2022-08-23 22:08:29
使用退出状态确定已安装和登录检查的成功/失败:
如果!命令-v gh >/dev/null 2>&1;然后回显“安装gh first”退出1 fi
如果!gh auth状态>/dev/null 2>&1;然后回显“您需要登录: gh auth登录”退出1 fi
https://stackoverflow.com/questions/73464747
复制相似问题