Bash:从一个引用字符串块中的每个带引号的字符串中创建一个变量
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bash:从一个引用字符串块中的每个带引号的字符串中创建一个变量相关的知识,希望对你有一定的参考价值。
我正在尝试编写一个脚本,它将遍历Shortcuts > Services
的Keyboard Preference Pane
部分中的每个服务,并禁用它。
这是禁用服务的基本命令:
defaults write pbs NSServicesStatus -dict-add SERVICE "
<dict>
<key>enabled_context_menu</key><false/>
<key>enabled_services_menu</key><false/>
<key>key_equivalent</key><string>""</string>
</dict>
"
提前获取所有服务并将它们放入脚本是低效的,因为在安装新软件时会逐步添加许多服务。它必须是某种循环。
到目前为止,我一直在尝试使用defaults
和awk
来获取包含服务的每一行。
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}" "
<dict>
<key>enabled_context_menu</key><false/>
<key>enabled_services_menu</key><false/>
<key>key_equivalent</key><string>""</string>
</dict>
"
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}" '
<dict>
<key>enabled_context_menu</key><false/>
<key>enabled_services_menu</key><false/>
<key>key_equivalent</key><string>""</string>
</dict>
'
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" "
<dict>
<key>enabled_context_menu</key><false/>
<key>enabled_services_menu</key><false/>
<key>key_equivalent</key><string>""</string>
</dict>"
'
当你想要调用程序defaults
时,可以删除第二个echo
。
echo "$b" | tr '{' '\n' | sed -rn 's/[^"]"(.*)".*/\1/p' |
xargs -I xxx defaults write pbs NSServicesStatus -dict-add "xxx" "
<dict>
<key>enabled_context_menu</key><false/>
<key>enabled_services_menu</key><false/>
<key>key_equivalent</key><string>""</string>
</dict>"
以上是关于Bash:从一个引用字符串块中的每个带引号的字符串中创建一个变量的主要内容,如果未能解决你的问题,请参考以下文章