简体   繁体   English

Shell脚本(macOS):从plist字符串打印转义的Unicode字符

[英]shell script (macOS): print escaped Unicode character from a plist string

In a shell script I'm reading strings from the Info.plist in application bundles, in this example from the Copyright key, whereas $FILEPATH is the path to an application bundle, eg /Applications/TextEdit.app 在外壳程序脚本中,我正在从应用程序捆绑包中的Info.plist中读取字符串,在本示例中是从版权键中读取字符串,而$ FILEPATH是应用程序捆绑包的路径,例如/Applications/TextEdit.app

#!/bin/bash  
PLIST=$(/usr/bin/defaults read "$FILEPATH/Contents/Info.plist")
COPYRIGHT=$(echo "$PLIST" | /usr/bin/awk -F" = " '/NSHumanReadableCopyright/{print $2}' RS=';' | /usr/bin/sed 's/^"\(.*\)"$/\1/')
echo "Copyright: $COPYRIGHT"

Note: I'm not reading the key directly, which would be easier, of course; 注意:我不是直接读密钥,这当然会更容易。 it's read into the variable PLIST, so the script can read other keys as well later on, and doesn't have to use the defaults command over and over. 它将被读入变量PLIST,因此脚本以后也可以读取其他键,而不必一遍又一遍地使用defaults命令。

Now the output is (for example) Copyright: Copyright \\\\U00a9 1995-2015, Apple Inc.\\\\nAll rights reserved. 现在的输出是(例如) Copyright: Copyright \\\\U00a9 1995-2015, Apple Inc.\\\\nAll rights reserved.

Obviously the copyright sign \\U00a9 was escaped, as was the line break, but how can I solve this problem so that the shell script actually prints out the string as it was meant to, ie with copyright sign and line break? 显然,版权符号\\ U00a9和换行符都被转义了,但是我该如何解决这个问题,以便shell脚本实际上按预期的方式打印出字符串,即带有版权符号和换行符?

PS: at the end I would probably remove the line break, but as the first step I'd want to echo the whole thing as intended. PS:最后,我可能会删除换行符,但是作为第一步,我想按预期回显整个内容。

I would to use plutil instead of the defaults . 我会使用plutil而不是defaults The: 该:

plist="/Applications/TextEdit.app/Contents/Info.plist"
plutil -p - < "$plist"

will print 将打印

... some lines deleted...
  "DTSDKName" => "macosx10.12internal"
  "DTXcode" => "0800"
  "NSHumanReadableCopyright" => "Copyright © 1995-2016, Apple Inc.
All rights reserved."
  "DTSDKBuild" => "16C7"
  "CFBundleDevelopmentRegion" => "English"
... other delted lines ...

the -p mean human readable format - so hard to process. -p表示人类可读的格式 -难以处理。 Therefore, is better convert the plist to json , for example: 因此,最好将plist转换为json ,例如:

plutil -convert json -r -o - - < "$plist"

the -r mean convert to human-readable JSON , eg -r表示转换为人类可读的JSON ,例如

{
  "CFBundleName" : "TextEdit",
  "DTSDKName" : "macosx10.12internal",
  "DTXcode" : "0800",
  "NSHumanReadableCopyright" : "Copyright © 1995-2016, Apple Inc.\nAll rights reserved.",
  "DTSDKBuild" : "16C7",
  "CFBundleDevelopmentRegion" : "English",
  "CFBundleVersion" : "329",

Now you can easily filter out the NSHumanReadableCopyright key, even using awk , but it is much better to use some real tool. 现在,即使使用awk ,您也可以轻松过滤掉NSHumanReadableCopyright键,但是使用一些实际工具会更好。

Mac by default has installed perl and also the JSON::PP module. Mac默认情况下安装了perl以及JSON::PP模块。 So the: 所以:

plist="/Applications/TextEdit.app/Contents/Info.plist"
plutil -convert json -r -o - - < "$plist" |\
/usr/bin/perl -0777 -CSDA -MJSON::PP -MEncode -E '$p=decode_json(encode_utf8(<>));say $p->{NSHumanReadableCopyright}'

will output 将输出

Copyright © 1995-2016, Apple Inc.
All rights reserved.

Edit: tested the whole as one long line from the comment: 编辑:从评论中将整个测试为一长行:

plist="/Applications/TextEdit.app/Contents/Info.plist"; jplist=$(/usr/bin/plutil -convert json -r -o - "$plist"); copyright=$(echo "$jplist" | /usr/bin/perl -0777 -CSDA -MJSON::PP -MEncode -E '$p=decode_json(encode_utf8(<>));say $p->{NSHumanReadableCopyright}'); echo "$copyright"

and prints OK... 并打印确定...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM