0

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.

5
  • when I do that, I get this: ✗ cat -v dockcsv.csv /Applications/Launchpad^M /Applications/Safari^M /Applications/Pages^M /Applications/Numbers^M /Applications/Keynote^M /Applications/Photos^M /Applications/iMovie^M /Applications/GarageBand^M /Applications/Microsoft Word^M /Applications/Microsoft Excel% Commented Oct 31, 2017 at 15:54
  • Clearly you've got CRLFs -- DOS-style newlines -- about. Commented Oct 31, 2017 at 15:56
  • 1
    BTW, see the very first item in the "Before Asking About Problematic Code" section of the bash tag wiki. Commented Oct 31, 2017 at 15:57
  • rookie mistake, this would have helped so much. noted and appreciated Commented Oct 31, 2017 at 16:03
  • 1
    Read these: mywiki.wooledge.org/DontReadLinesWithFor and mywiki.wooledge.org/BashFAQ/001 Commented Oct 31, 2017 at 16:34

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

3 Comments

echo -n is needlessly nonportable -- shells aren't required to do anything with -n other than print it as a string on stdout. Safer to use printf '%s' "$i" to print $i with no newline. See pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html, particularly the APPLICATION USAGE and RATIONALE sections.
That said, since you're already using printf, you could use printf ' <string>%s</string>\n' "$i". Not that that'll help with the OP's problem (caused by stray carriage returns).
I agree with you, I have write to fast
0

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

4 Comments

yeah I'm getting the ^M, installing dos2unix now and will report back.
If you don't have dos2unix, plenty of other ways to fix this. Opening the file in vim and running :set fileformat=unix before saving it will do the trick.
wow that actually fixed it for me... I never would have guessed that to be the case. Is this common?
Yes, if the file was created on a Windows system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.