简体   繁体   中英

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

#!/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.

Now the output is (for example) 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?

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.

I would to use plutil instead of the 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. Therefore, is better convert the plist to json , for example:

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

the -r mean convert to human-readable JSON , eg

{
  "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.

Mac by default has installed perl and also the JSON::PP module. 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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