Reputation: 35
Ran into another roadblock. I'm to generate text that looks like the following.
<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>
It's not too complex, but here is the code used to generate this:
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>"
where $1 is a csv file that a user specifies. A column contains a list of application locations:
/Applications/Launchpad
/Applications/Safari
/Applications/Pages
/Applications/Numbers
/Applications/Keynote
/Applications/Photos
/Applications/iMovie
/Applications/GarageBand
/Applications/Microsoft Word
/Applications/Microsoft Excel
When I run the script without the variable, I get this:
<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>
But if I run the command with the variable printed, it causes it to act weird EXCEPT for the last value in the loop.
.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>
I've tried tricking it, but once I get it in the way that i want it, it acts weird. Any help would be appreciated.
Upvotes: 0
Views: 27
Reputation: 4967
Replace loop for by 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"
Upvotes: 1
Reputation: 88583
Check your csv file for non-printable characters: cat -A file.csv
or cat -v file.csv
If you find something like ^M
, then I recommend: dos2unix file.csv
Upvotes: 0