使用jq输出作为变量存储bash命令

时间:2019-06-03 17:36:51

标签: json bash jq

我需要将以下bash命令存储为变量。这似乎是一个简单的概念,但每次运行都无法正常工作。

下面是我已经尝试过的代码和JSON结构。

getSessions ()
{

    sessions=dcv list-sessions --json
    user=echo "${!sessions}" | jq '.[] .owner | tr -d '"''
    connections=echo "${!sessions}" | jq '.[] ."num-of-connections"'
    lastDisconnect=echo "${!sessions}" | jq '.[] ."last-disconnection-time"' | tr -d '"' | sed 's/.\{17\}$//'''
}

printf "$user\n$connections\n$lastDisconnect"

end;
[
  {
    "id" : "123456",
    "owner" : "johndoe",
    "x11-display" : ":9",
    "x11-authority" : "/run/user/1112/dcv/123456.xauth",
    "num-of-connections" : 0,
    "creation-time" : "2019-05-28T14:42:24.027240Z",
    "last-disconnection-time" : "2019-05-30T21:47:36.682935Z"
  },
  {
    "id" : "12345",
    "owner" : "johnsmith",
    "x11-display" : ":5",
    "x11-authority" : "/run/user/user/dcv/12345.xauth",
    "num-of-connections" : 0,
    "creation-time" : "2019-05-14T14:12:14.989287Z",
    "last-disconnection-time" : "2019-05-31T18:58:42.851223Z"
  },
]

我总是收到意想不到的文件错误结尾,好像它无论如何用引号引起来都无法转义该命令一样。我希望输出像:

johndoe
0
2019-05-30

0 个答案:

没有答案