我目前正在使用 i3 并希望使用
i3-msg -t get_workspaces
解析可见工作区,以“保存工作区”并在我的番茄钟休息结束时返回到它们,问题是我以前从未在 bash 中使用过 json (除了一个小时前的几个简单例子之外)。
基本上我正在尝试返回可见工作区的名称值。
[
{
"id": 94475011596992,
"num": 6,
"name": "6:NoteTaking",
"visible": false,
"focused": false,
"rect": {
"x": 40,
"y": 65,
"width": 1840,
"height": 975
},
"output": "DisplayPort-2",
"urgent": false
},
{
"id": 94475011603104,
"num": 7,
"name": "7:Entertainment",
"visible": false,
"focused": false,
"rect": {
"x": 40,
"y": 65,
"width": 1840,
"height": 975
},
"output": "DisplayPort-2",
"urgent": false
},
{
"id": 94475011612464,
"num": 9,
"name": "9",
"visible": true,
"focused": true,
"rect": {
"x": 40,
"y": 65,
"width": 1840,
"height": 975
},
"output": "DisplayPort-2",
"urgent": false
},
{
"id": 94475011631328,
"num": 1,
"name": "1:Browsing",
"visible": true,
"focused": false,
"rect": {
"x": 40,
"y": 1145,
"width": 1840,
"height": 975
},
"output": "DisplayPort-1",
"urgent": false
},
{
"id": 94475011637712,
"num": 2,
"name": "2:Messaging",
"visible": false,
"focused": false,
"rect": {
"x": 40,
"y": 1145,
"width": 1840,
"height": 975
},
"output": "DisplayPort-1",
"urgent": false
},
{
"id": 94475011644096,
"num": 3,
"name": "3",
"visible": false,
"focused": false,
"rect": {
"x": 40,
"y": 1145,
"width": 1840,
"height": 975
},
"output": "DisplayPort-1",
"urgent": false
}
]
目前我用这条生产线承担大部分繁重的工作,
local currentWorkspaces=$(i3-msg -t get_workspaces | jq -r 'map(select(.visible == true)).name')
它返回正确的值(假设它们在引号中。)
[
"9",
"1:Browsing"
]
我一直在尝试用一个简单的数组来调用它们(也从未使用过数组,所以可能会把事情搞砸)
i3-msg workspace "$currentWorkspaces[@]"
目前我不知道如何继续前进,任何帮助将不胜感激(-r 我认为会返回不带引号的值的选项并没有这样做)
假设.name中的值没有空格或某些其他字符, 您将能够摆脱以下困境:
X=$(i3-msg -t get_workspaces | jq -r '.[] | select(.visible == true).name' )
for var in $X[@]
do
echo "$var"
done
输出:
9
1:Browsing[@]