1

I am trying to create files from an array called columnHeaders[] Within the array, I have test values that are currently: id, source, EN, EN-GB, French-FR, French-DE

When I run my code I get

EN

EN.xml

EN-GB

EN-GB.xml

French-FR

French-FR.xml

French-DE

.xmlch-DE

NOTE that the FRENCH-DE filename gets morphed into .xmlch-DE Why is this? I can't for the life of me figure out whey only that file ends up looking like this. It's driving me crazy! Thanks for any help.

below is the snippet of my code that is causing me problems:

# take all the languages and either find the files or create new files for them. The language     options
# should be stored in the columnHeader array in positions 3 - n


# cycle through all the output languages (so exclude "id, source' inputs)
# in my example, numWordsInLine is 6
c=2
while [ $c -lt $numWordsInLine ]; do
    OUTPUT_LANG="${columnHeaders[$c]}"
    echo "$OUTPUT_LANG"
    OUTPUT_FILE="$OUTPUT_LANG".xml

    # HERE'S WHERE YOU CAN SEE OUTPUT_FILE IS WRONG FOR FRENCH_DE
    echo "$OUTPUT_FILE"

    OUTPUT_BAK="$OUTPUT_LANG".bak
    TMP_FILE="~tmp.xml"

    if [ -f "$OUTPUT_BAK" ]; then
            rm "$OUTPUT_BAK"
    fi
    # make a backup of the original language.xml file in case of program error or interruption
    if [ -f "$OUTPUT_FILE" ]; then
            mv "$OUTPUT_FILE" "$OUTPUT_BAK"
    fi

    if [ -f "$TMP_FILE" ]; then
    rm "$TMP_FILE"
    fi

    c=$(expr $c + 1)
done
8
  • Show output of echo ${columnHeaders[@]} Commented Sep 21, 2014 at 18:57
  • code: echo "Hi stack overflow" echo ${columnHeaders[@]} output: Hi stack overflow id source EN EN-GB French-FR French-DE Commented Sep 21, 2014 at 19:11
  • 1
    @Cyrus, without more quotes that isn't helpful. "${columnHeaders[@]}", not ${columnHeaders[@]}, or you get the same result as you would from ${columnHeaders[*]}. Commented Sep 21, 2014 at 19:48
  • 1
    ...also, if you wanted to be able to see nonprintable characters, you'd use printf %q, not %s. Commented Sep 21, 2014 at 19:49
  • 1
    @kusold, re: code review -- see the automated tool at shellcheck.net, which can catch a great many common errors. Commented Sep 21, 2014 at 19:49

1 Answer 1

2

I'm betting that you are reading that line of data from a file with DOS newlines.

I'm also betting that the contents of the variable are "fine" but include a trailing carriage return.

Try printf %q\\n "$OUTPUT_FILE" or echo "$OUTPUT_FILE" | cat -v to see.

Then use something like dos2unix on the file to convert it.

Extra (unrelated) comments:

There's also no reason to use expr. ((c++)) will do what you want.

You could even turn the loop itself into for ((c=2;c < $numWordsInLine; c++)); do if you wanted to.

$numWordsInLine is also unnecessary if $columnHeaders is already split into the right "words" since you can use ${#columnHeaders} to get the length.

Sign up to request clarification or add additional context in comments.

5 Comments

aha! I believe this is correct. When I tried that I ended up getting: 'French-DE\r.xml' Is there anyway to remove the \r without using dos2unix?
also thanks for your code review comments. This is my first bash script and I have been piecing together bits of information. Your comments are really helpful
You can manually strip it from the file entries if you want to with something like ${OUTPUT_FILE%$'\r'} but that's more work and you have to be sure to do that every time (or do it once at input/read time and save the modified value). Is there a reason you don't want to modify the file?
@kusold: add columnHeaders=(${columnHeaders[@]%$'\r'}) before your while loop to remove DOS newlines from your array elements.
@Cyrus You need to be careful with quoting. In this case it doesn't matter but that suggestion will word split all the elements of columnHeaders. columnHeaders=("${columnHeaders[@]%$'\r'}") will avoid that.

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.