需要使用字符串/变量进行修补

发布于 2024-09-12 10:47:47 字数 263 浏览 3 评论 0原文

我正在编写一个脚本来自动化在计算机实验室中设置一堆 Mac 的过程。

每个系统都是唯一标识的,我需要一种方法,使用相同的字符串在多个位置修补 plist 文件,该字符串将从脚本中的用户读取 这是一个 bash 脚本

原始字符串始终是相同的。修补字符串是可变的,具体取决于运行脚本的系统的身份。该字符串是在脚本开始时出于各种其他目的从用户处读取的,并存储在 $macnum 中。

有人可以为我提供一个可以编写脚本来执行任务的简单解决方案吗?谢谢。

I am writing a script to automate the process of setting up a bunch of Mac's in a computer lab.

Each system is uniquely identified and I need a method of patching a plist file in several locations with the same string that will be read from the user in the script which is a bash script

The original string is always the same. The patching string is variable depending on the identity of the system the script is being run on. This string is read from the user at the start of the script for various other purposes and stored in $macnum.

Can anybody please provide me a simple solution that can be scripted to perform the task? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-09-19 10:47:47

您可以在plist中使用一些唯一标识符(例如{{MACHINE_ID}})并使用sed来替换它:

sed -i -e 's/{{MACHINE_ID}}/'"$macnum"/g filename

You can use some unique identifier (e.g. {{MACHINE_ID}}) in the plist and use sed to replace it:

sed -i -e 's/{{MACHINE_ID}}/'"$macnum"/g filename
〃温暖了心ぐ 2024-09-19 10:47:47
sed -i "s/plist-macnum-placeholder/$macnum/g' file ...

其中 -i 表示“就地”编辑文件,而 /g 表示每行进行多次替换,如果只有一次则可以删除。

sed -i "s/plist-macnum-placeholder/$macnum/g' file ...

Where -i means edit the file "in-place" and /g says make the substitution multiple times per line and can be dropped if there is only one.

半岛未凉 2024-09-19 10:47:47

如果您要更改的 plist 是 XML 格式,则 strager 和 msw 给出的基于 sed 的方法可以正常工作,但如果它是 Apple 的二进制格式,则可能会损坏文件格式。您可以先使用 plutil 将其转换为 XML:

plutil -convert xml1 filename
sed -i -e "s/placeholder/$macnum/g" filename

之后不必将其转换回二进制格式,因为 Apple 的 plist 框架可以互换地读取这两种格式。另一种方法是使用 PlistBuddy 编辑 plist 的内容(尽管它需要脚本知道将哪些条目设置为哪些值,而不是仅仅替换占位符):

/usr/libexec/PlistBuddy -c "set :oneentry 'value including $macnum where appropriate'" filename
/usr/libexec/PlistBuddy -c "set :anotherentry 'value including $macnum where appropriate'" filename

最后,您可以使用默认值执行相同的操作,尽管它要求您通过完整路径指定 .plist 文件,并保留 .plist 的名称:

defaults write oneentry "value including $macnum where appropriate" /path/to/filename-without-plist
defaults write anotherentry "value including $macnum where appropriate" /path/to/filename-without-plist

The sed-based approach strager and msw gave will work fine if the plist you're changing is in XML format, but if it's in Apple's binary format it'll probably corrupt the file format. You can use plutil to convert it to XML first:

plutil -convert xml1 filename
sed -i -e "s/placeholder/$macnum/g" filename

It shouldn't be necessary to convert it back to binary format afterward, as Apple's plist frameworks read the two formats interchangeably. Another approach would be to use PlistBuddy to edit the contents of the plist (although it'll require the script to know what entries to set to what values, rather than just replacing a placeholder):

/usr/libexec/PlistBuddy -c "set :oneentry 'value including $macnum where appropriate'" filename
/usr/libexec/PlistBuddy -c "set :anotherentry 'value including $macnum where appropriate'" filename

Finally, you can do the same thing with defaults, although it requires you specify the .plist file by full path, and leave the .plist off its name:

defaults write oneentry "value including $macnum where appropriate" /path/to/filename-without-plist
defaults write anotherentry "value including $macnum where appropriate" /path/to/filename-without-plist
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文