Sunday, 21 September 2014

Bash file creation from variable



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

No comments:

Post a Comment