我正在尝试检查用户是否已登录以使用 Docker
。从基本搜索中,我发现当我执行cat ~/.docker/config.json
时,如果我得到:
{
"experimental" : "disabled",
"stackOrchestrator" : "swarm",
"auths" : {
},
"credsStore" : "desktop"
}
当用户登录时,如果我得到:
{
"stackOrchestrator" : "swarm",
"experimental" : "disabled",
"credsStore" : "desktop",
"auths" : {
"https://index.docker.io/v1/" : {
}
}
}
所以我存储在一个变量中:'docker_login_info=$(command cat ~/.docker/config.json)'
但我发现很难在 shell 脚本中读取这个嵌套映射并根据 auths 内部有一个值的条件来决定它。我很感激这方面的一些指导。
答案 0 :(得分:1)
如果 auths
为空,则此命令将输出 0,如果 auths
有一些条目,则该命令将输出 0:
jq '.auths | length' ~/.docker/config.json
如果你需要一个变量的输出,你可以试试这个:
docker_login_info=`jq '.auths | length' ~/.docker/config.json`
然后您可以在 shell 中像这样使用变量:
[ $docker_login_info -eq 0 ] && echo 'User not logged it' || echo 'User logged in'
可以在此处找到有关 jq
的更多信息:https://stedolan.github.io/jq/