热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:Bash:从一个引用字符串块中的每个带引号的字符串中创建一个变量

篇首语:本文由编程笔记#小编为大家整理,主要介绍了Bash:从一个引用字符串块中的每个带引号的字符串中创建一个变量相关的知识,希望对你有一定的参考价值。 我正在尝试编写一个脚本,它将遍历Shortcu

篇首语:本文由编程笔记#小编为大家整理,主要介绍了Bash:从一个引用字符串块中的每个带引号的字符串中创建一个变量相关的知识,希望对你有一定的参考价值。



我正在尝试编写一个脚本,它将遍历Shortcuts > ServicesKeyboard Preference Pane部分中的每个服务,并禁用它。

这是禁用服务的基本命令:

defaults write pbs NSServicesStatus -dict-add SERVICE "

enabled_context_menu
enabled_services_menu
key_equivalent""

"

提前获取所有服务并将它们放入脚本是低效的,因为在安装新软件时会逐步添加许多服务。它必须是某种循环。

到目前为止,我一直在尝试使用defaultsawk来获取包含服务的每一行。

a=$(defaults read pbs NSServicesStatus | awk 'NR%5==2' ) && echo $a

而且,这是输出:

"com.ScooterSoftware.BeyondCompare - Compare Files - bcCompareFiles" = { "com.ScooterSoftware.BeyondCompare - Compare Folders - bcCompareFolders" = { "com.ScooterSoftware.BeyondCompare - Select Left File for Compare - bcSelLeftFile" = { "com.ScooterSoftware.BeyondCompare - Select Left Folder for Compare - bcSelLeftFolder" = { "com.apple.BluetoothFileExchange - Send File To Bluetooth Device - sendFileUsingBluetoothOBEXService" = { "com.apple.ChineseTextConverterService - Convert Text from Simplified to Traditional Chinese - convertTextToTraditionalChinese" = { "com.apple.ChineseTextConverterService - Convert Text from Traditional to Simplified Chinese - convertTextToSimplifiedChinese" = { "com.apple.ChineseTextConverterService - Convert Text to Full Width - convertTextToFullWidth" = { "com.apple.ChineseTextConverterService - Convert Text to Half Width - convertTextToHalfWidth" = { "com.apple.Dictionary - Look Up in Dictionary - doLookupService" = { "com.apple.FolderActionsSetup - Folder Actions Setup - openFilesFromPasteboard" = { "com.apple.Grab - Capture Selection from Screen - variableSelection" = { "com.apple.ImageCaptureService - Import Image - ImportImage" = { "com.apple.QuickTime.service.encodeSelectedAudioFiles - Encode Selected Audio Files - runWorkflowAsService" = { "com.apple.Safari - Add to Reading List - addToReadingList" = { "com.apple.Safari - Search With %WebSearchProvider@ - searchWithWebSearchProvider" = { "com.apple.ScriptEditor2 - Script Editor/Get Result of AppleScript - runAsAppleScript" = { "com.apple.SpotlightService - SEARCH_WITH_SPOTLIGHT - doSearchWithSpotlight" = { "com.apple.Stickies - Make Sticky - makeStickyFromTextService" = { "com.apple.Terminal - New Terminal Tab at Folder - newTerminalAtFolder" = { "com.apple.Terminal - New Terminal at Folder - newTerminalAtFolder" = { "com.apple.Terminal - Open man Page in Terminal - openManPage" = { "com.apple.Terminal - Search man Page Index in Terminal - searchManPages" = { "com.apple.finder - Finder/Open - open" = { "com.apple.finder - Finder/Reveal - reveal" = { "com.apple.finder - Finder/Show Info - showInfo" = { "com.apple.mail - Mail/New Email To Address - mailTo" = { "com.apple.mail - Mail/New Email With Selection - mailSelection" = { "com.apple.services.addToiTunesAsSpokenTrack - Add to iTunes as a Spoken Track - runWorkflowAsService" = { "com.apple.services.encodeSelectedVideoFiles - Encode Selected Video Files - runWorkflowAsService" = { "com.apple.services.setDesktopPicture - Set Desktop Picture - runWorkflowAsService" = { "com.apple.services.showMap - Show Map - runWorkflowAsService" = { "com.apple.systemuiserver - Open URL - openURL" = { "com.evernote.Evernote - ENApplicationServiceMenuItemTitle - serviceCreateNote" = { "com.flexibits.cardhop.mac - Send to Cardhop - sendToCardhop" = { "com.flexibits.fantastical2.mac - Send to Fantastical 2 - sendToFantastical" = { "com.houdah.HoudahSpot4 - New HoudahSpot Search - newSearch" = { "com.houdah.HoudahSpot4 - Search Folders in HoudahSpot - folderSearch" = { "com.houdah.HoudahSpot4 - Search in HoudahSpot - search" = { "com.kapeli.dashdoc - Create Snippet in Dash - createSnippetService" = { "com.kapeli.dashdoc - Look Up in Dash - lookupService" = { "net.pornel.ImageOptim - ImageOptimize - handleServices" = { }

每个引用的字符串代表服务。

这是我的问题:

我试图用这个=命令删除所有无关的数据和空格({}sed),但无法摆脱那个单独的尾随}

b=$(echo $a | sed -E -e 's/ = { / /g' ) && echo "${b}"

即使没有尾随的}(无论如何只会在最后一次迭代中产生错误,或者我相信),在我所做的循环中没有正确解析单个字符串:

for service in "${b}"; do
defaults write pbs NSServicesStatus -dict-add "${service}" "

enabled_context_menu
enabled_services_menu
key_equivalent""

"
done

我仅使用以下命令进行测试,但未获得有效结果:

/usr/libexec/PlistBuddy -c "Print :NSServicesStatus:${service}" ~/Library/Preferences/pbs.plist

似乎只有服务被正确解析,我不知道哪一个:

Dict {
key_equivalent =
enabled_context_menu = false
enabled_services_menu = false
}

我该如何解决这个问题?

谢谢!


答案

我实际上并不宽容解析或使用结构不知道的工具生成结构化数据,但忽略了 -

while IFS= read -r service; do
service=${service%'"'}; service=${service#'"'}
defaults write pbs NSServicesStatus -dict-add "${service}" '

enabled_context_menu
enabled_services_menu
key_equivalent""

'
done <<(egrep -o '"[^"]+"' <<<"$a")

另一答案

当我正确理解您的数据时,您可以获得服务

echo "$b" | tr '{' '\n' | sed -rn 's/[^"]"(.*)".*/\1/p'

您可以使用回声结构defaults ...

echo "$b" | tr '{' '\n' | sed -rn 's/[^"]"(.*)".*/\1/p' |
xargs -I xxx echo 'defaults write pbs NSServicesStatus -dict-add "xxx" "

enabled_context_menu
enabled_services_menu
key_equivalent""
"
'

当你想要调用程序defaults时,可以删除第二个echo

echo "$b" | tr '{' '\n' | sed -rn 's/[^"]"(.*)".*/\1/p' |
xargs -I xxx defaults write pbs NSServicesStatus -dict-add "xxx" "

enabled_context_menu
enabled_services_menu
key_equivalent""
"


推荐阅读
author-avatar
159dzhqian449_734
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有