进入另一个障碍。我生成的文本如下所示。
<dict>
<key>cfurl_string</key>
<string>/Applications/Launchpad.app</string>
</dict>
<dict>
<key>cfurl_string</key>
<string>/Applications/Safari.app</string>
</dict>
<dict>
<key>cfurl_string</key>
<string>/Applications/Photos.app</string>
</dict>
它不是太复杂,但这是用于生成此代码的代码:
for i in $(cut -d "," -f1 $1); do
printf '<dict>\n'
printf ' <key>cfurl_string</key>\n'
printf ' <string>'
echo -n "$i"
printf '</string>\n'
printf '</dict>\n'
done
echo "</array>"
其中$ 1是用户指定的csv文件。列包含应用程序位置列表:
/Applications/Launchpad
/Applications/Safari
/Applications/Pages
/Applications/Numbers
/Applications/Keynote
/Applications/Photos
/Applications/iMovie
/Applications/GarageBand
/Applications/Microsoft Word
/Applications/Microsoft Excel
当我运行没有变量的脚本时,我得到了这个:
<array>
<dict>
<key>cfurl_string</key>
<string></string>
</dict>
<dict>
<key>cfurl_string</key>
<string></string>
</dict>
<dict>
<key>cfurl_string</key>
<string></string>
</dict>
<dict>
<key>cfurl_string</key>
<string></string>
</dict>
<dict>
<key>cfurl_string</key>
<string></string>
</dict>
<dict>
<key>cfurl_string</key>
<string></string>
</dict>
<dict>
<key>cfurl_string</key>
<string></string>
</dict>
<dict>
<key>cfurl_string</key>
<string></string>
</dict>
<dict>
<key>cfurl_string</key>
<string></string>
</dict>
<dict>
<key>cfurl_string</key>
<string></string>
</dict>
</array>
但是如果我运行带有变量print的命令,则会导致它对循环中的最后一个值起作用。
.app</string>lications/GarageBand
</dict>
<dict>
<key>cfurl_string</key>
.app</string>lications/Microsoft Word
</dict>
<dict>
<key>cfurl_string</key>
<string>/Applications/Microsoft Excel.app</string>
</dict>
</array>
我试过欺骗它,但是一旦我按照我想要的方式得到它,它就会很奇怪。任何帮助,将不胜感激。
答案 0 :(得分:1)
用while替换循环:
while IFS="," read i
do
printf '<dict>\n'
printf ' <key>cfurl_string</key>\n'
printf ' <string>'
printf ' <string>%s</string>\n' "$i"
printf '</string>\n'
printf '</dict>\n'
done < "$1"
printf "</array>\n"
答案 1 :(得分:0)
检查您的csv文件中是否有不可打印的字符:cat -A file.csv
或cat -v file.csv
如果您发现^M
之类的内容,我建议您:dos2unix file.csv