如何使用单个`defaults`命令定义数组?


3

我想要一个域名 com.apple.keyboard.modifiermapping.1452-628-0 包含以下数组:

(
        {
        HIDKeyboardModifierMappingDst = 2;
        HIDKeyboardModifierMappingSrc = 0;
    },
        {
        HIDKeyboardModifierMappingDst = 0;
        HIDKeyboardModifierMappingSrc = 2;
    },
        {
        HIDKeyboardModifierMappingDst = 0;
        HIDKeyboardModifierMappingSrc = 10;
    }
)

我可以通过删除当前值并运行三个来实现它 -array-add 命令:

defaults -currentHost delete -g com.apple.keyboard.modifiermapping.1452-628-0
defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array-add '{ HIDKeyboardModifierMappingDst = 2; HIDKeyboardModifierMappingSrc = 0; }'    
defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array-add '{ HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 2; }'    
defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array-add '{ HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 10; }'
defaults -currentHost read -g com.apple.keyboard.modifiermapping.1452-628-0

我可以使用什么语法在单个命令中定义数组?


  • 如果我运行以下脚本:

    defaults -currentHost delete -g com.apple.keyboard.modifiermapping.1452-628-0
    defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array '(
      { HIDKeyboardModifierMappingDst = 2; HIDKeyboardModifierMappingSrc = 0; },
      { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 2; },
      { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 10; } )' 
    defaults -currentHost read -g com.apple.keyboard.modifiermapping.1452-628-0
    

    我在一个数组中得到一个数组:

    (
            (
                    {
                HIDKeyboardModifierMappingDst = 2;
                HIDKeyboardModifierMappingSrc = 0;
            },
                    {
                HIDKeyboardModifierMappingDst = 0;
                HIDKeyboardModifierMappingSrc = 2;
            },
                    {
                HIDKeyboardModifierMappingDst = 0;
                HIDKeyboardModifierMappingSrc = 10;
            }
        )
    )
    
  • 如果我尝试 -plist 代替 -array 我得到了 Unexpected argument

  • 如果我运行以下脚本:

    defaults -currentHost delete -g com.apple.keyboard.modifiermapping.1452-628-0    
    defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array '
      { HIDKeyboardModifierMappingDst = 2; HIDKeyboardModifierMappingSrc = 0; },
      { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 2; },
      { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 10; }'
    defaults -currentHost read -g com.apple.keyboard.modifiermapping.1452-628-0
    

    我收到语法错误 defaults[22044:679428] Could not parse: ... Try single-quoting it.


再次提问:我可以使用什么语法在单个命令中定义数组?

Answers:


2

-array 将单个数组元素作为单独的参数。

defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 -array \
  '{ HIDKeyboardModifierMappingDst = 2; HIDKeyboardModifierMappingSrc = 0; }' \
  '{ HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 2; }' \
  '{ HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 10; }'

'\'用于保持shell等待下一行,因此如果写在一行上,它们可以省略。你不需要使用 delete 第一:不像 -array-add-array 用新内容替换整个数组。

如果您不想这样做,可以选择以下方法:您可以将数据结构作为字符串传递。这更接近您问题中的示例,并且工作原理相同。

defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-628-0 '(
  { HIDKeyboardModifierMappingDst = 2; HIDKeyboardModifierMappingSrc = 0; },
  { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 2; },
  { HIDKeyboardModifierMappingDst = 0; HIDKeyboardModifierMappingSrc = 10; } )' 

这完全回答了我的问题。但是,我是否应该用一个论点提出另一个问题?我的目的是提供单一的数据结构,在编写问题时,我没有意识到“单一命令”和“单一数据结构”是可以通过不同方式实现的要求。
techraf

1
@techraf我编辑了我的答案,包括一个单字符串方法,看看是否更好
grg

哦!现在我懂了。 -array-dict 是可选的。我试图说服它不存在 -plist... 谢谢!
techraf
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.